Parsing instanceof in JavaScript may return true for different constructors

Source: Internet
Author: User

Parsing instanceof in JavaScript may return true for different constructors

This article mainly parses the instanceof in JavaScript for different constructors that may return true in detail. If you need it, you can refer to it for help.

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. If the object obj is created through new Constructor, obj instanceof Constructor is true.

 

 

The Code is as follows:

Function Person (n, ){

This. name = n;

This. age =;

}

Var p = new Person ('John backus', 82 );

Console. log (p instanceof Person); // true

 

2. If there is an inheritance relationship, then the subclass instance instanceof parent class will return true

The Code is as follows:

Function (){}

Function B (){}

B. prototype = new A (); // B inherits from

 

Var B = new B ();

Console. log (B instanceof A); // true

 

3. Because the Object is the root class and all other custom classes inherit from it, the instanceof Object of any constructor returns true.

Copy the Code as follows:

Function (){}

Var a = new ();

Console. log (a instanceof Object); // true

 

Var str = new String ('hello ');

Console. log (str instanceof Object); // true

 

Var num = new Number (1 );

Console. log (num instanceof Object); // true

 

Even the constructor itself

The Code is as follows:

Function (){}

Console. log (A instanceof Object); // true

Console. log (String instanceof Object); // true

Console. log (Number instanceof Object); // true

 

4. All constructors, instanceof Function, return true

The Code is as follows:

Function (){}

Console. log (A instanceof Function); // true

Console. log (String instanceof Function); // true

Console. 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, then 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.

 

 

The Code is as follows:

Class Person {

Public String name;

Public int age;

Person (String n, int ){

This. name = name;

This. age =;

}

Public static void main (String [] args ){

Person p = new Person ("John Backus", 82 );

System. out. println (p instanceof Person); // true

System. out. println (p instanceof Object); // true

}

}

 

If there is an inheritance relationship in Java, then the subclass instance instanceof parent class also returns true

The Code is as follows:

// Parent class

Class Person {

Public String name;

Public int age;

Person (String n, int ){

Name = name;

Age =;

}

}

// Subclass

Public class Man extends Person {

Public String university;

Man (String n, int a, String s ){

Super (n, );

University = s;

}

Public static void main (String [] args ){

Man mm = new Man ("John Resig", 29, "PKU ");

System. out. println (mm instanceof Man); // true

System. out. println (mm instanceof Person); // true

}

}

 

With this in mind, it is not surprising to see the following in JS:

Copy the Code as follows:

// Define two Constructors

Function (){}

Function B (){}

A. prototype = B. prototype = {a: 1 };

 

// Create two instances with different constructors

Var a = new ();

Var B = new B ();

Console. log (a instanceof B); // true

Console. log (B instanceof A); // true

 

We can see that a and B are created with A and B respectively, but a instanceof B and B instanceof A are both 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.

 

 

The Code is as follows:

Function (){}

Var a = new ();

A. prototype = {}; // modify the prototype dynamically. Note that the prototype must be created after a is created.

Console. log (a instanceof A); // false

 

Note that this writing breaks the first article in the above summary: The object obj is created through new Constructor, so obj instanceof Constructor is true.

 

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.

 

 

The Code is as follows:

Var a, B = 1, c = true, d = 'hello ';

Console. log (a instanceof Object); // false the value is undefined.

Console. log (B instanceof Object); // false

Console. log (c instanceof Object); // false

Console. 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,

The Code is as follows:

Function (){}

A. prototype = 1; // prototype of A is set to non-Object Type

Var a = new ();

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.