Class syntax help me understand

I passed the lesson, but please help me understand if I got right.

  **Your code so far**

// Only change code below this line
class Vegetable { // make a class Vegetable
constructor (Vegetable) { // initializing the object Vegetable
this.name = Vegetable; // this refers to the object inside function (Vegetable), gives name property a value Vegetable
}
}
// Only change code above this line

const carrot = new Vegetable('carrot'); // making a new objective called carrot, new is copying the values of Vegetable objective and assigns new changes.
console.log(carrot.name); // Should display 'carrot' // carrot.name is now carrot.

I have added notes next to the commands tell me if I am right / wrong please ty :slight_smile:

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Use class Syntax to Define a Constructor Function

Link to the challenge:

Well, if you passed the test, then you probably got it right, right?

I mean, what you have works, I just have one qualm:

  constructor (Vegetable) {

I would not call that variable “Vegetable”.

  1. We already have a class named that. That might cause confusion.
  2. In JS, we usually reserve PascalCase for classes and object constructors. For “normal” variables, we use camelCase.
  3. It is not really the vegetable, but the “name”.

It seems to work, but that is unnecessarily confusing.

If you need more information than that, please ask a specific question.

Thanks, I understand constructor (name) { this.name = name;
is simpler and less confusing.

Shouldn’t it be Name Tho?

But “name” is neither a class nor an object constructor. It’s just a simple variable as a parameter.

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