What does 'this' mean and how class work

** How class working and what is this word.


// Only change code below this line
class Vegetable { 
constructor(name) {
  this.name = name;
}
}
// Only change code above this line

const carrot = new Vegetable('carrot');
console.log(carrot.name); // Should display 'carrot'
**Your browser information:**

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

Challenge: Use class Syntax to Define a Constructor Function

Link to the challenge:

what word are you asking about

I am asking for β€˜this’ word.

new keyword is creating an instance of Vegetable class i.e. carrot.
in such process , carrot is inheriting all properties of class like name.
this refer to the class itself. like in current case class Vegetable

1 Like

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