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 aclass
or Constructor FunctionInstantiation
โ (process) to create new Object Instance fromclass
CC
โ Reactclass
Instanceextends
React.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 inclass
definitions are not initializedSimplicity
โ function are simpler to compose thanclass
which applies more Abstractionprivate
โ JS Keyword & Property Modifier to restrict Method to withinclass
onlyEventEmitter
โclass
constructor to createemitter
InstanceStatic Method
โ Method attach toclass
only & NOT InstanceInstance Private Field
โclass
field property with#
prefix to store privately within Instanceprotected
โ Modifier to restrict Method toclass
or child classesstatic
โ JS Keyword to define Static Method/property forclass
constructor()
โ specialclass
Method to pass args & setup new new Instance