[i]Learn· Using classes
Public fields
Private fields also have their public counterparts, which allow every instance to have a property. Fields are usually designed to be independent of the constructor's parameters.
class MyClass {
luckyNumber = Math.random();
}
console.log(new MyClass().luckyNumber); // 0.5
console.log(new MyClass().luckyNumber); // 0.3
Public fields are almost equivalent to assigning a property to this. For example, the above example can also be converted to:
class MyClass {
constructor() {
this.luckyNumber = Math.random();
}
}example 1 of 2
[>_]Terminal
Output appears here when you press Run.
$ ▌