why instanceof keeps saying true after prototype changed?

独自空忆成欢 提交于 2021-02-20 13:51:56

问题


The instanceof operator should look at the prototype, no? Why does it not change its answer after the object's prototype has been changed? Example below:

// The .prototype of objects created with 'new MyKlass'
// is MyKlass.prototype
var MyKlass = function(name, age) {
  this.name = name;
  this.age = age;
}

var xx = new MyKlass('xx', 20);
console.log(xx instanceof MyKlass);      // true, OK

xx.prototype = new String('s');
console.log(xx instanceof MyKlass);      // also true, WHY???

回答1:


This case is explained in the MDN :

Note that if the value of an instanceof test can change based on changes to the prototype property of constructors, it cannot be changed by changing an object prototype, because changing an object prototype is not possible in standard ECMAScript. It is however possible using the non-standard __proto__ pseudo-property

This would log false :

xx.constructor.prototype = new String('s');
console.log(xx instanceof MyKlass);

In short, you shouldn't try to mutate JavaScript objects, they weren't designed to be mutable. I don't know what's your use case but there's probably a better solution, be it composition, internal state, or something's else.




回答2:


It does not look at .prototype but [[prototype]], or what is available in some browsers as .__proto__

xx.__proto__ = new String("s");
console.log(xx instanceof MyKlass); 
//false
console.log(xx instanceof String); 
//true

Assigning a .prototype property to a non function has no effect except the normal assignment of any normal property really. And for functions that only has an effect when the function is used in instanceof check or called with new.




回答3:


The instanceof operator should look at the prototype, no?

Yes, it does. See the MDN docs.

Why does it not change its answer after the object's prototype has been changed?

var xx = new MyKlass('xx', 20);
xx.prototype = new String('s');

Because you didn't change the prototype of your xx object, but gave it a prototype property. Object.getPrototypeOf(xx) === MyKlass.prototype still applies. See __proto__ VS. prototype in JavaScript for details. What would work:

MyKlass.prototype = {}; // overwrite with a different object
console.log(xx instanceof MyKlass); // false now, xx doesn't inherit from the {}

or

xx.__proto__ = String.prototype; // or something
console.log(xx instanceof MyKlass); // false now, xx doesn't inherit from MyKlass.prototype

Notice that writing to the internal [[prototype]] via __proto__ is non-standard in ES5



来源:https://stackoverflow.com/questions/17343905/why-instanceof-keeps-saying-true-after-prototype-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!