Introduction to the instanceof operator
In JavaScript, to judge the type of a variable to try the typeof operator, it is a problem to store a value using a reference type when using the TypeOf operator, regardless of what type of object is referenced, it returns "Object". ECMAScript introduced another Java operator instanceof to solve the problem. The instanceof operator is similar to the typeof operator and is used to identify the type of object being processed. Unlike the typeof approach, the Instanceof method requires the developer to explicitly confirm that the object is a specific type. For example:
Listing 1. Instanceof Sample
var ostringobject = new String ("Hello World");
Console.log (Ostringobject instanceof String); Output "true"
The code asks, "is the variable ostringobject an instance of a String object?" "Ostringobject is indeed an instance of a String object, so the result is" true ". Although not as flexible as the TypeOf method, the Instanceof method is useful when the TypeOf method returns "Object".
General usage of the instanceof operator:
Generally speaking, using instanceof is to judge whether an instance is of a certain type. For example:
Listing 2. instanceof General Usage
Determine if Foo is an instance of the Foo class
function foo () {}
var foo = new Foo ();
Console.log (foo instanceof foo)//true
In addition, the heavier point is that instanceof can be used in an inheritance relationship to determine whether an instance belongs to its parent type. For example:
Listing 3. The use of instanceof in relationships in inheritance
Determine if Foo is an instance of the Foo class and whether it is an instance
function Aoo () {}
function foo () {}
Foo.prototype = new Aoo () for its parent type;//javascript Prototype Inherits
var foo = new Foo ();
Console.log (foo instanceof foo)//true
console.log (foo instanceof aoo)//true
The above code is a judgment of the parent class in a layer of inheritance, in which the instanceof operator is also applicable.
Do you really know the instanceof operator?
Looking at the code example above, do you think the instanceof operator is very simple, and here's a bit of a complex usage.
Listing 4. instanceof Complex usage
Console.log (Object instanceof object);//true
console.log (function instanceof function);//true
Console.log ( Number instanceof number);//false
Console.log (string instanceof string);//false
Console.log (Function instanceof Object);//true
console.log (foo instanceof Function);//true
console.log (foo instanceof foo); False
Look at the above code is not again dizzy? Why is Object and Function instanceof themselves equal to true, while other classes instanceof themselves not equal to true? How to explain? To fundamentally understand the mysteries of instanceof, you need to start with two aspects: 1, how the operator is defined in the language specification. 2,javascript prototype inheritance mechanism.
Listing 5. JavaScript instanceof Operator Code
function Instance_of (l, R) {//l represents the left expression, R represents the right expression
var O = r.prototype;//takes R's display prototype
L = l.__proto__;//takes L's implicit prototype
while (True) {
if (L = = null) return
false;
if (o = = L)//Here The emphasis: Returns True when O is strictly equal to L
;
L = l.__proto__;
}
}
Listing 6. Object Instanceof Object
To facilitate presentation, first distinguish between the left expression and the right expression
Objectl = object, Objectr = object;
Below according to the specification stepwise deduction
O = Objectr.prototype = Object.prototype
L = objectl.__proto__ = Function.prototype
//First judgment C7/>o!= L
//Loops find L/l
= function.prototype.__proto__ = Object.prototype
//second judgment
O = = l< c12/>//returns True
Listing 7. function instanceof function
To facilitate presentation, first distinguish between left and right expressions
functionl = function, Functionr = function;
Below according to the specification step-by-Step deduction
O = Functionr.prototype = Function.prototype
L = functionl.__proto__ = Function.prototype
First judgment
O = L
//return True
Listing 8. Foo instanceof foo
To facilitate presentation, first differentiate between left and right expressions
fool = foo, foor = foo;
Below according to the specification stepwise deduction
o = Foor.prototype = foo.prototype
L = fool.__proto__ = Function.prototype
//First judgment
o!= l
//Loop again find L whether there are __proto__
L = function.prototype.__proto__ = Object.prototype
//second judgment
O!= L
//again follow Ring lookup L
= __proto__ L = object.prototype.__proto__ = null
//Third judgment
L = = null
//return False
A brief analysis of the application of instanceof in the Dojo inheritance mechanism
In JavaScript, there is no multiple inheritance of this concept, just like Java. However, when you use declare to declare a class in Dojo, you are allowed to inherit from multiple classes. The following is an example of Dojo 1.6.1.
Listing 9. Multiple inheritance in Dojo
Dojo.declare ("Aoo", null,{});
Dojo.declare ("Boo", null,{});
Dojo.declare ("Foo", [aoo,boo],{});
var foo = new Foo ();
Console.log (foo instanceof aoo);//true
console.log (foo instanceof Boo);//false
Console.log ( Foo.isinstanceof (AOO));//true
Console.log (foo.isinstanceof (Boo));//true
In the example above, Foo inherits both from Aoo and Boo, but returns false when the instanceof operator is used to check whether Foo is an instance of boo. In fact, within Dojo, Foo is still only inherited from Aoo, and the mixin mechanism copies the methods and properties in the Boo class to Foo, so it returns false when the instanceof operator is used to check for a boo instance. So Dojo adds a new method for each instance of the class called Isinstanceof, which examines multiple inheritance in this way.