Skip to content

Instantly share code, notes, and snippets.

@donmccurdy
Last active May 5, 2022 16:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save donmccurdy/5bf0c4e71b7590a629540515f4e31ed1 to your computer and use it in GitHub Desktop.
Initialization order and public class properties

Initialization order and public class properties

(A)

class Parent {
    constructor() {
        console.log(`${this.type}::constructor`)
    }
}

Parent.prototype.type = 'parent';

class Child extends Parent {
    constructor() {
        super();
        console.log(`${this.type}::constructor`)
    }
}

Child.prototype.type = 'child';

const child = new Child();
// → "child::constructor"
// → "child::constructor"

(B)

class Parent {
    type = 'parent';
    constructor() {
        console.log(`${this.type}::constructor`)
    }
}

class Child extends Parent {
    type = 'child';
    constructor() {
        super();
        console.log(`${this.type}::constructor`)
    }
}

const child = new Child();
// → "parent::constructor"
// → "child::constructor"

NOTICE: As of May 2022, modifications to .prototype disable tree-shaking for the entire class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment