Consider the following ES6 Classes:
'use strict';
class Dummy {
}
class ExtendDummy extends Dummy {
constructor(...args) {
super(...args)
}
}
class ExtendString extends String {
constructor(...args) {
super(...args)
}
}
const ed = new ExtendDummy('dummy');
const es = new ExtendString('string');
console.log(ed instanceof ExtendDummy);
console.log(es instanceof ExtendString);
My understanding is that both should be true
, and in Firefox and Chrome they are, however Node says es instanceof ExtendString
is false
. It's the same with other constructors, not just String
.
Software I used:
- Node v5.11.0 with the
--harmony
flag. - Chrome 50
- Firefox 45
Which JavaScript engine is correct and why?
Node appears to be incorrect,
es instanceof ExtendString
should definitely betrue
(like everyone would expect).String[Symbol.hasInstance]
is not overwritten, andObject.getPrototypeOf(es)
should beExtendedString.prototype
, as the spec details this in theString (value)
function description:The newtarget refers to
ExtendString
when you construct thenew ExtendString('string')
instance, and since it is a constructor with a.prototype
object it will useExtendedString.prototype
not%StringPrototype
as the [[prototype]] for the newly created exotic String object: