Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active November 14, 2019 06:55
Show Gist options
  • Save rauschma/3f2ff93b5698cdf9362d7e5b477351a3 to your computer and use it in GitHub Desktop.
Save rauschma/3f2ff93b5698cdf9362d7e5b477351a3 to your computer and use it in GitHub Desktop.
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