I am trying to understand a simple class declaration.
The following worked for a lesson but where is the class field "name" defined?
class Vegetable{
constructor (name){
this.name = name;
}
}
Classes in JavaScript are just another way of writing functions and using prototypes. What you’ve got there is just
function Vegetable(name) {
this.name = name;
}
The ‘class’ fields are defined by the constructor. Public and private field declarations like you may be familiar with from other languages are coming, but are currently in the experimental stage and not fully supported everywhere.
1 Like