Closed jonlepage closed 4 years ago
That's not related to optional chaining, n1.isFinite()
also returns false
.
This happens because when you access n1.isFinite
, the number primitive gets converted ("boxed") to a Number object and, in loose mode, your isFinite
method is then called with this
being the "boxed" Number object instead of the primitive number 100
.
Three ways you can get around this
Number.isFinite
return Number.isFinite(Number(this))
Number.prototype.isFinite = function() {
'use strict';
return Number.isFinite(this)
}
Anyway, not an optional chaining issue.
I have nothing to add to @noppa’s answer, except that, if I had to give “three ways you can get around this”, they would be:
Strict mode eliminates the trap you've fallen into (namely, autoboxing of primitive values when used as target of method calls), and resolves other gotchas as well.
(I echo the recommendation)
To clarify, strict mode doesn't change autoboxing in the general case - (100).toString()
, eg, works the same in any mode. What strict mode does do is make assigning to a property on an autoboxed primitive an error, rather than a silent failure; it also avoids autoboxing a receiver, so that the this
will be the primitive value in strict mode (where in sloppy mode, it would be the boxed object).
I'm not sure this has any relation to optional chaining :-)
I very love this new addon. But something strange append in some case. Should no more need manually type Object wrapper in some case with this addon. Example Number Test case