Object? Class? Property? Key?

have i made an object called Vegetable? or carrot?
Vegetable is a class which represents an object. but carrot looks like an object in output. Vegetable looks like a function. i fink.
(i passed the excercise, more or less copying, but i don’t understand this)

class Vegetable {
  constructor(name,color,len,form) {
    this.name = name, this.length=len,this.shape=form,this.color=color;
  }
}

const carrot = new Vegetable("carrot",'orangy',5,'pokey');
console.log(carrot.name); 
console.log(carrot)
console.log(Vegetable)

CONSOLE:
carrot
Vegetable {name: "carrot", length: 5, shape: "pokey", color: "orangy"}
class Vegetable {
  constructor(name,color,len,form) {
    this.name = name, this.length=len,this.shape=form,this.color=color;
  }
}

(above is from VScode)

Well, they are all objects. In JS, any data that isn’t a primitive is some kind of an object.

More specifically, I would say that Vegetable is a class. Yes, technically it is an object (at least in JS). It is actually a function (a type of object in JS) that creates an object. It is syntactic sugar for the old JS object constructor (also a function so also an object).

“carrot” is an object. It is an instance of the class “Vegetable”.

So, in a more high-level sense, “Vegetable” is a class and “carrot” is an instance of that class, basically a “plain” object. (I hope I got all of that right.)

makes sense.
but but it still looks like i gave ‘name: carrot’ as a property of the object, ‘Vegetable’.
but i should consider Vegetable a ‘class’ and ‘carrot’ an object, as an instance?

also, are the other (instantiations?) i created seperate instances of the Vegetable class? i was trying to make them properties of carrot, but perhaps they are merely seperate instances of Vegetable?
(hope that question makes more sense to you than it does for me. I might have to come back to this when the swelling goes down a bit)

Yes, you gave the class a name property and gave it a string that matches the name of the variable that references it. You don’t have to do that - you just chose to do that. The instance doesn’t know or care that the name property matches the name of this variable that references it.

i was trying to make them properties of carrot, but perhaps they are merely seperate instances of Vegetable?

Yes, each instance of the class is a separate thing. They are like “children” of that class. They are individual siblings. They will have a similar structure of keys/properties, but the values/data in those are in no way connected.

(hope that question makes more sense to you than it does for me. I might have to come back to this when the swelling goes down a bit)

This is confusing stuff. Go easy on yourself. We all struggled with this at one time.

1 Like

thanks, bud.
it’s stopped bleeding

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.