Created
April 16, 2021 18:18
-
-
Save bathos/ce349aa7710e2509b2046c549f9e22c0 to your computer and use it in GitHub Desktop.
demonstration of the consequence of whether [[ConstructorKind]] is "base" or "derived"
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
// 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