Uh oh!
There was an error while loading. Please reload this page.
fix(__extends): Use correct behaviour with null prototype - #85
fix(__extends): Use correct behaviour with null prototype#85ExE Boss (ExE-Boss) wants to merge 1 commit into
null prototype#85Conversation
Holger Jeromin (HolgerJeromin)
commented
Jan 15, 2020
Does this change relate to a change in Typescript? Or should there be a change there? |
ExE Boss (ExE-Boss)
commented
Jan 15, 2020
There should also be a change in TypeScript. |
| d.prototype = Object.create(b); | ||
| return; | ||
| } | ||
| extendStatics(d, b); |
There was a problem hiding this comment.
Why not just do this instead?
| extendStatics(d,b); | |
| if(b!==null)extendStatics(d,b); |
There was a problem hiding this comment.
Because it’s necessary to also add the d.prototype.constructor property in the b === null case.
There was a problem hiding this comment.
Your proposed change at the time I wrote that comment also did not set the constructor when b === null. Given your point about constructor, I'd probably recommend something simpler (and with fewer characters overall):
__extends=function(d,b){if(typeofb!=="function"&&b!==null)thrownewTypeError("Class extends value "+String(b)+" is not a constructor or null");if(b)extendStatics(d,b);function__(){}(d.prototype=b ? (__.prototype=b.prototype,new__()) : Object.create(b)).constructor=d;};A few notes:
- I'm using
if (b)rather thanif (b !== null)since the null check happens on the first line and a function's "truthiness" can't be overridden throughvalueOf,Symbol.toPrimitive, or aProxy - I swapped the condition on the last line from
b === nulltob, and therefore swapped the order of expressions in the conditional. - I moved the
constructorassignment out of the__prototype helper and onto the last line.
There was a problem hiding this comment.
It might be better to fold the extendStatics call inside the b ? conditional to lower the amount of ToBoolean(b) checks necessary:
function__extends(d,b){// This should also check that `b.prototype` is an object:if(typeofb!=="function"&&b!==null)thrownewTypeError("Class extends value "+String(b)+" is not a constructor or null");function__(){}(d.prototype=b
? (extendStatics(d,b),__.prototype=b.prototype,new__())
: Object.create(b)).constructor=d;}f073ee5 to
b04b8d0CompareCo-authored-by: Ron Buckton <ron.buckton@microsoft.com>
b04b8d0 to
5f74ae1CompareRon Buckton (rbuckton)
commented
Feb 8, 2022
Apparently "correct" behavior when extending Until this has been resolved in the specification, I'd rather not make any more changes to |
If you were to call
__extends(Foo, null), it would cause incorrect behaviour forextendStatics, asFoois supposed to inherit from%Function.prototype%in that case, instead of also inheriting fromnull.