What is the best way to recognize that obj2 and obj3 are different?
That will depend a great deal on what you're doing with them. One way would be to use instanceof Obj2
and instanceof Obj3
. Since both objects were created with Obj1.prototype
in their prototype chain, it makes sense that they identify as being an instance of what we would call the supertype in class-based OOP.
How does actually instanceof work?
The short version
obj instanceof F
looks to see if the object referenced by F.prototype
is anywhere in obj
's prototype chain. It doesn't use constructor
at all.
More details
This is covered in the spec by §11.8.5 - The instanceof Operator, which says (indirectly, via §8.6.2) that it calls the [[HasInstance]]
internal method of the function object, passing in the object we're testing. Function
's [[HasInstance]]
(in §15.3.5.3) says that it gets the object reference from the function's prototype
property and then returns true
if that object is anywhere in the target object's prototype chain, false
if it doesn't.
It doesn't use constructor
(nothing in JavaScript itself does, in fact)?— and if you think about it, it can't, because an object's constructor
property can only point at one function, but an object can be instanceof
multiple functions?— for instance, in the case of pseudo-classical inheritance:
function F1() {}
function F2() {
F1.call(this);
}
F2.prototype = Object.create(F1.prototype);
F2.prototype.constructor = F2;
var obj = new F2();
console.log(obj instanceof F1); // true
console.log(obj instanceof F2); // true
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…