#classes and objects

1 messages · Page 1 of 1 (latest)

still remnant
#

just to clarify and check if i understand it or not , in js for example this code , its classes with object inside ?

  title;
  description;
  dueDate;
  owner;
  isDone;
  constructor(title = '', description = '', owner = new this.owner()) {
    this.title = title;
    this.description = description;
    this.owner = owner;
  }
}

class Onwer {
  firstName;
  lastName;
  role;

  constructor(firstName, lastName, role) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.role = role;
  }
}```
clear frigate
#

Not exactly. A class is like a blueprint for creating objects, and an object is an instance of that class. It might be a bit confusing at first, but think of it this way: the class defines the structure or design of an object, and an object is when you actually use that blueprint to create something with real values.

still remnant
#

so i think i got it just watend to verify

#

because

#

i made a class for employee in general and i created an employee object

class :

  firstName;
  lastName;
  role;
  salary;```

object 
```const worker1 = new Employee('Moshe', 'Levi', 'Software Developer', 20000);

correct ?

clear frigate
#

What you have inside a class are attributes (similar to variables) and methods (similar to functions)

For example, your Task class has the following attributes:

  • title
  • description
  • dueDate
  • owner
  • isDone

You don't have any methods but you have a contributor, which is mechanism that is used to pass the values to the class and create an object out of them.

still remnant
#

because during the whole class im telling to myself , but why the object i know from earlier is this :

name: 'John',
last name: 'Doe',
id: 341413
};```

so what's the diff ?
clear frigate
#

So, JavaScript can be a bit confusing when you're learning Object-Oriented Programming (OOP). At first, it wasn't really an object-oriented language. Instead, it started with just objects that were essentially simple collections of key-value pairs. This means that objects in JavaScript were originally just a way to store data (like { name: 'John', lastName: 'Doe' }), with no built-in way to define behaviors or methods that the object could perform.

In your example, the person object is just a collection of properties (name, lastName, etc.), with no functions attached to it. It's like a data container, not something that has actions it can do.

Later, JavaScript introduced classes, which are like blueprints for creating these objects but with additional features. A class allows you to define both data (attributes) and behavior (methods) together. So instead of just having a simple object that stores data, a class lets you create objects that can also do things, like a greet() method that prints a message based on the object’s attributes.

So, when you’re learning OOP in JavaScript, it can get confusing because it started out as just key-value pairs, and only later introduced classes to formalize the OOP structure.

#

What you showed is basically the simplest way to define an object in JavaScript, but as you will see later on, it will only contain values, not behavior (ex. methods)

#

The main goal of classes and objects is to organise data and behavior. Instead of having random bits of data and actions all over the place, you group them together.

#

I hope it make sense? It's a quirk of js and I agree that it's confusing.

still remnant
#

reading now , sorry for taking time ❤️

clear frigate
#

Don't worry about it

#

You answer when you can, I just want to make sure you understand correctly

still remnant
#

so instead (and i will say it in my own words) , of just sotring data and value , its now store a "parents" and the objects are the "children" kind of

clear frigate
#

Not exactly. It's more like you have a variable that contains many other variables

#

But with classes it contains behavior, or actions or "functions", as well

still remnant
#

its a variable that contains the object and its connected to the class , like if u want to describe a football team , there is a stadium name , football club name , year of establish , head coach , and then in the variable you complete it

clear frigate
#
class Person {
  name;

  constructor(name) {
    this.name = name
  }

  greet() {
    console.log(`Hello ${this.name}!`)
  }
}

const p = new Person("NugByte")
p.greet() // Hello NugByte
prisma sunBOT
#
Program Output
Hello NugByte!

still remnant
#

our teacher said that this is the hardset part of js , and tbh i really like this concept 🙂

still remnant
#

@clear frigate

clear frigate