Last active
November 14, 2019 06:55
-
-
Save rauschma/3f2ff93b5698cdf9362d7e5b477351a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert'); | |
class A { | |
constructor() { | |
return {self: this}; | |
} | |
} | |
class B extends A { | |
#prop = 'hello'; | |
constructor() { | |
super(); | |
assert.equal(this.#prop, 'hello'); | |
assert.equal(this instanceof B, false); | |
assert.equal(this.self instanceof B, true); | |
return this.self; | |
} | |
getProp() { | |
return this.#prop; | |
} | |
} | |
assert.throws( | |
() => new B().getProp(), | |
/^TypeError: Cannot read private member #prop from an object whose class did not declare it$/ | |
); | |
/* | |
This example is contrived. But something similar happens | |
if you return a Promise for `this` from a constructor. | |
Note that this is the expected behavior and in accordance with | |
how instances are set up in class hierarchies: | |
https://exploringjs.com/es6/ch_classes.html#sec_allocating-and-initializing-instances | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment