javascript - why instanceof keeps saying true after prototype changed? - Stack Overflow
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???
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???
Share
Improve this question
edited Jun 27, 2013 at 13:22
Denys Séguret
383k90 gold badges811 silver badges777 bronze badges
asked Jun 27, 2013 at 13:01
zpzpzpzp
753 bronze badges
3 Answers
Reset to default 9This 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 position, internal state, or something's else.
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
.
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
- 百度推出智能云云手机
- 虚拟现实究竟离现实还有多远?
- 微软每年从安卓厂商获20亿美元专利费
- python - tensorflow-gpu installation failed in colab - Stack Overflow
- Why has VS Code stopped discovering my python tests? - Stack Overflow
- android - Segmentation Fault at start up of Maui App - Stack Overflow
- reveal.js - Why do the divs have odd formating and non-matching colors in revealjs Quarto presentation? - Stack Overflow
- postgresql - How to use prisma updateManyupsert in order to update multiple data with one database query? - Stack Overflow
- python - NaN values in Pandas are not being filled by the interpolate function when it's applied to a full dataframe - S
- javascript - The side menu bar(sticky) is overflowing unless I scroll - Stack Overflow
- python - Pyinstaller (Mac App) - Why only permission given to the executable file (instead of .app bundle) could work? - Stack O
- javascript - How to play HLS live-stream from the end with Bitmovin player - Stack Overflow
- node.js - TypeError: Cannot read properties of undefined (reading 'server') while calling socket.io in nextjs -
- google cloud platform - Querying public google_ads_transparency_center BQ table doesn't show all the needed data - Stack
- innodb - How does MySQL handle lock queuing order for SELECT ... FOR UPDATE? - Stack Overflow
- Spring restcontroller with RequestParam String conflcting in controller - Stack Overflow
- How to Use GraalVM to Execute JavaScript Code Making REST Calls Using Java? - Stack Overflow