class โ JS Keyword to declare Class (as Syntactic Sugar over Constructor Function)
aka ES6-Class
class Person {
#fName // instance private field
instanceProp = 123
constructor(fName) {
this.#fName = fName
}
greet = () => console.log(`Hi ${this.#fName}`)
static printNames = (persons) => console.log(persons.map(p => p.#fName))
}
const bob = new Person("Bob")
bob.greet()
Person.printNames([bob, new Person("jack")])
class is more compact JS Syntax to set up Prototype Chain
Instance Private Fieldโ
class field property with# prefix to store privately within Instance
Static Methodโ
Method attach to class only & NOT Instance
Subclassโ
constructor()โ
special class Method to pass args & setup new new Instance
describe()
Instantiate via new
const bob = new myClass("Bob", 18);
Referencesโ
Instanceโ Object created through aclassor Constructor FunctionInstantiationโ (process) to create new Object Instance fromclassCCโ ReactclassInstanceextendsReact.Component, provides render() Method to return UI, manage state & perform side effects on Lifecycle Methods--strictPropertyInitializationโ flag to set TSC to throw error when properties inclassdefinitions are not initializedSimplicityโ function are simpler to compose thanclasswhich applies more Abstractionprivateโ JS Keyword & Property Modifier to restrict Method to withinclassonlyEventEmitterโclassconstructor to createemitterInstanceStatic Methodโ Method attach toclassonly & NOT InstanceInstance Private Fieldโclassfield property with#prefix to store privately within Instanceprotectedโ Modifier to restrict Method toclassor child classesstaticโ JS Keyword to define Static Method/property forclassconstructor()โ specialclassMethod to pass args & setup new new Instance