JavaScript Constructor Function es6 part 16

Опубликовано: 10 Март 2026
на канале: edtech by meera
443
19

The constructor method is a special method for creating and initializing an object created with a class.
Use the class keyword and write a constructor to create the Vegetable class.
The Vegetable class allows you to create a vegetable object with a property name that gets passed to the constructor.

As per requirement of the test following points to note for doing this task

Vegetable should be a class with a defined constructor method.

The class keyword should be used.

Vegetable should be able to be instantiated.

carrot.name should return carrot

```
//change code below
class Vegetable {
constructor(name){
this.name = name;

}

}
//change code above

const carrot = new Vegetable("carrot");
console.log(carrot.name);

```
So, we created a class called Vegetable that has details of Vegetable object. Then we need to put a constructor with a parameter of name into this object and set it to this.object.

Subscribe to my channel , Click on the bell icon to get notified of my latest tutorials.
This educational channel is dedicated to teach coding, web development, programming concepts and tips for absolute beginners.

SUBSCRIBE to this channel if you want to become a web developer without any formal degree in computer science .

Click below to Subscribe :    / edtechbymeera  

Click here to share this video:
   • JavaScript Constructor Function es6 part 1...  

Follow me on social media:

Github:
https://github.com/meeramenon07

Medium:
  / edtechbymeera  

codepen :
https://codepen.io/meeramenon07

Jsitor:
https://jsitor.com/profile/meeramenon07

Facebook:
  / edtechbymeera  

Instagram:
@meeraloves2code

Twitter:
  / edtechbymeera  

Get my eBook on Gumroad:

https://edtechbymeera.gumroad.com/

https://ko-fi.com/meeramenon