Skip to content

Instantly share code, notes, and snippets.

@bathos
Created April 16, 2021 18:18
Show Gist options
  • Save bathos/ce349aa7710e2509b2046c549f9e22c0 to your computer and use it in GitHub Desktop.
Save bathos/ce349aa7710e2509b2046c549f9e22c0 to your computer and use it in GitHub Desktop.
demonstration of the consequence of whether [[ConstructorKind]] is "base" or "derived"
// CLASS SYNTAX ////////////////////////////////////////////////////////////////
class AFoo {}
class ABar extends AFoo {}
// FUNCTION SYNTAX NEAR-EQUIV //////////////////////////////////////////////////
function BFoo() {
if (!new.target) {
throw new TypeError('Illegal constructor');
}
}
function BBar() {
if (!new.target) {
throw new TypeError('Illegal constructor');
} else {
return Reflect.construct(BFoo, arguments, new.target);
}
}
// OBSERVABLE DIFFERENCE ///////////////////////////////////////////////////////
let aInstanceInitializationCount = 0;
let bInstanceInitializationCount = 0;
Reflect.construct(ABar, [], new Proxy(function () {}.bind(), {
get(target) {
aInstanceInitializationCount++;
return {};
}
}));
Reflect.construct(BBar, [], new Proxy(function () {}.bind(), {
get(target) {
bInstanceInitializationCount++;
return {};
}
}));
console.log({
aInstanceInitializationCount,
bInstanceInitializationCount
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment