We know that the instanceof operator is used to check whether the object is an instance of a constructor. The following lists the scenarios in which true is returned.
1,Object obj is created through new constructor.OBJ instanceof ConstructorIsTrue
Function person (n, a) {This. name = N; this. age = A;} var P = new person ('John backus', 82); console. log (P instanceof person); // true
2. If there is an inheritance relationshipSubclass instance instanceof parent classReturns true.
Function A () {} function B () {} B. prototype = new A (); // B inherits from Avar B = new B (); console. log (B instanceof A); // true
3. Because the object is the root class and all other custom classes inherit from itInstanceof object of any ConstructorReturns true.
Function A () {} var A = new A (); console. log (A instanceof object); // truevar STR = new string ('hello'); console. log (STR instanceof object); // truevar num = new number (1); console. log (Num instanceof object); // true
Even the constructor itself
Function A () {} console. Log (A instanceof object); // trueconsole. Log (string instanceof object); // trueconsole. Log (number instanceof object); // true
4,All constructors instanceof FunctionReturns true.
Function A () {} console. Log (A instanceof function); // trueconsole. Log (string instanceof function); // trueconsole. Log (number instanceof function); // true
The above four points are summarized as one sentence:If an instance is created through a certain class or its subclass, instanceof returns true.. Or if the prototype of a constructor is linked to the internal prototype of the object OBJ, true is returned. That is, the results of instanceof are not directly related to the constructor itself. This is common in many languages.
A person class is defined in Java. instance P returns true for both person and object.
Class person {public string name; Public int age; person (string N, int A) {This. name = Name; this. age = A;} public static void main (string [] ARGs) {person P = new person ("John Backus", 82); system. out. println (P instanceof person); // truesystem. out. println (P instanceof object); // true }}
If there is an inheritance relationship in JavaSubclass instance instanceof parent classReturns true.
// Parent class person {public string name; Public int age; person (string N, int A) {name = Name; age = ;}} // subclass public class man extends person {Public String University; man (string N, int A, string s) {super (n, a); University = s ;} public static void main (string [] ARGs) {MAN Mm = new man ("John resig", 29, "PKU"); system. out. println (MM instanceof man); // truesystem. out. println (MM instanceof person); // it is also true }}
With this in mind, it is not surprising to see the following in JS:
// Define two constructors: function a () {} function B () {}. prototype = B. prototype = {A: 1}; // create two instances with different constructors, var a = new A (); var B = new B (); console. log (A instanceof B); // trueconsole. log (B instanceof A); // true
We can see that A and B are created with A and B respectively,A instanceof BAndB instanceofAll are true. That is, although a is not created by constructor B, true is returned. Because B. prototype exists in the internal prototype chain of.
Due to the Dynamic Language Features of JS, you can modify the prototype at runtime. Therefore, it is not surprising that false is returned below. Because a. prototype is no longer in the internal prototype chain of A, the chain is interrupted.
Function A () {} var A = new A ();. prototype = {}; // modify the prototype dynamically. Note that the console must be created after a is created. log (A instanceof A); // false
Note that this writing breaks the first article in the above summary:Object obj is created through new constructor.OBJ instanceof ConstructorIsTrue
In the actual ecmascript standard (subject to 5.1), the internal implementation of instanceof calls the internal method of the constructor [[hasinstance]. The description is as follows:
If F is a function object, the following steps will occur when F (v) is executed:
1. If the value of instanceof left operator V is not of the object type, false is directly returned.
VaR a, B = 1, C = true, D = 'hello'; console. log (A instanceof object); // false here, the value is undefinedconsole. log (B instanceof object); // falseconsole. log (C instanceof object); // falseconsole. log (d instanceof object); // false
2/3. Take the prototype attribute of constructor F. If it is not an object type, a typeerror exception must be thrown,
Function A () {} A. Prototype = 1; // prototype of a is set to non-object type var A = new A (); console. Log (A instanceof );
Different browsers throw different prompts for exceptions,
Firefox18:
Chrome24:
Safari6:
Opera12:
Ie10:
4. Continuously execute the following logic: set V as the internal prototype v. If V is null, false is returned. If both V and O point to the same object, true is returned.
Related:
Https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Operators/instanceof
Relationship between _ PROTO _ and prototype in Javascript