So, I was writing some example code implementing another function for Constructor[Symbol.hasInstance]
and I noticed my new implementation just won't get called.
The script below is what I expected to happen:
function Pirate(name) {
this.name = name;
}
const jackSparrow = {
isPirate: true
};
// Notice how `jackSparrow` is not yet considered an instance of the `Pirate` object
console.log(jackSparrow instanceof Pirate); // false
// Now let's assign another function for `Pirate[Symbol.hasInstance]`
Pirate[Symbol.hasInstance] = function (anObj) {
return anObj.isPirate;
};
// This will cause Pirate[Symbol.hasInstance] to be called with `jackSparrow`
console.log(jackSparrow instanceof Pirate); // true
I tried to add a console.log
call to to my Pirate[Symbol.hasInstance] implementation, but it won't log anything to the console.
Does anyone have any idea of what is happening? Why is my implementation not getting called?
I'm running this on Node 6.9.1.