JSFiddle - React, Tailwind, and code Playground
by SanjayVyas
JavaScript
//% Latest JS standard allows private instance and static members
//% However, there is a GOTCHA -> private methods are part of every object
//# [Sanjay Vyas]
class Person {
//! Private fields are declared with # are part of every object
#id
name
//~* class methods go into prototype and they dont have further prototype
print() {
console.log(`id=${this.#id}, name=${this.#name}`);
}
//~! #methods do NOT goto prototypes, they are part of every object
#private_method() {
console.log(`id=${this.#id}, name=${this.#name}`);
}
constructor(id, name) {
this.#id = id;
this.#name = name;
}
printPrivate() {
this.#private_method();
}
}
let bigB = new Person(1, "Brendan Eich");
bigB.print(); //> id=1, name=Brendan Eich
bigB.printPrivate(); //> id=1, name=Brendan Eich