Skip to main content

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

Class Expression

Subclassโ€‹

child class extends class

constructor()โ€‹

special class Method to pass args & setup new new Instance

describe()

Instantiate via new

const bob = new myClass("Bob", 18);

extends

Referencesโ€‹

  1. Instance โ†” Object created through a class or Constructor Function

  2. Instantiation โ†” (process) to create new Object Instance from class

  3. CC โ†” React class Instance extends React.Component, provides render() Method to return UI, manage state & perform side effects on Lifecycle Methods

  4. --strictPropertyInitialization โ†” flag to set TSC to throw error when properties in class definitions are not initialized

  5. Simplicity โ†” function are simpler to compose than class which applies more Abstraction

  6. private โ†” JS Keyword & Property Modifier to restrict Method to within class only

  7. EventEmitter โ†” class constructor to create emitter Instance

  8. Subclass โ†” child class extends class

  9. Static Method โ†” Method attach to class only & NOT Instance

  10. Instance Private Field โ†” class field property with# prefix to store privately within Instance

  11. protected โ†” Modifier to restrict Method to class or child classes

  12. static โ†” JS Keyword to define Static Method/property for class

  13. constructor() โ†” special class Method to pass args & setup new new Instance