๐Ÿ„class

๐Ÿšง under construction

JS โŸฉ value โŸฉ object โŸฉ class

(declaration) โญ๏ธ ES6 (2015)

creates a new class and gives it a name (identifier).

// โญ๏ธ class definition
class A { 

    // โญ๏ธ instance members  (# means "private")
    publicProp;                     // this.publicProp = undefined (by default)
    publicMethod() { ... }          // this.publicMethod()
    
    #privateProp;                   // this.#privateProp
    #privateProp2 = 0;
    
    get #privateGetter() { ... }    // this.#privateGetter
    #privateMethod() { ... }        // this.#privateMethod()
    
    // โญ๏ธ static members
    static staticProp;              // A.staticProp
    
    // โญ๏ธ if omitted, a default (empty) constructor is created automatically.
    constructor() { ... }
}

// โญ๏ธ class expression
let B = class { ... }

โญ class declaration is not hoistedโ—

a class's prototype (property) is read-onlyโ—

โญ๏ธโญ๏ธโญ๏ธ you can change the prototype of a function, but not that of a class.

// overwrite a function's prototype is OK
func.prototype = {}     // โœ… OK

// overwrite a class's prototype won't work
Class.prototype = {}    // โŒ won't work (ignored silentlyโ—๏ธ)

โ›” ReferenceError: cannot access '...' before initialization.

an error mostly caused by referencing a let/ const/ class in its temporal dead zoneโ—

using typeof on lexical declaration (let/ const/ class) in its temporal dead zone will throw a ReferenceError.

// โญ temporal dead zone (start)
// ------------------------------
// typeof aLet;       // โ›” ReferenceError: Cannot access 'aLet' before initialization
// typeof aConst;     // โ›” ReferenceError: Cannot access 'aConst' before initialization
// typeof aClass;     // โ›” ReferenceError: Cannot access 'aClass' before initialization

typeof undeclared;        // โ— 'undefined'

// โญ lexical declarations (let/const/class)
// --------------------------------------------
let aLet;                 // โญ initialized to `undefined`
const aConst = "hello";
class aClass {}

Last updated

Was this helpful?