Parsing JavaScript instanceof may return True_javascript tips for different constructors

Source: Internet
Author: User
Tags inheritance

We know that the instanceof operator is used to check whether an object is an instance of a constructor. The following lists the various scenarios in which it returns true.

1. Object obj was created through new constructor, then obj Instanceof constructor is true

Copy Code code as follows:

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 relationship, then the subclass instance instanceof the parent class also returns true
Copy Code code as follows:

function A () {}
function B () {}
B.prototype = new A (); b inherits from a

var B = new B ();
Console.log (b instanceof A); True

3, because the Object is the root class, all other custom classes inherit from it, so the instance instanceof object of any constructor returns true
Copy Code code as follows:

function A () {}
var a = new A ();
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
Copy Code code as follows:

function A () {}
Console.log (A instanceof Object); True
Console.log (String instanceof Object); True
Console.log (number instanceof Object); True

4, all constructors instanceof Function returns True
Copy Code code as follows:

function A () {}
Console.log (A instanceof Function); True
Console.log (String instanceof Function); True
Console.log (number instanceof Function); True

The above four points summed up as a sentence: If an instance is created from a class or its subclasses, then Instanceof returns True. Or the prototype of a constructor exists on the inner prototype chain of object obj, and returns True. That is, the result of instanceof is not directly related to the constructor itself. This is common in many languages.

A class person is defined in Java, and instance p returns true for both person and object

Copy Code code as follows:

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); True
SYSTEM.OUT.PRINTLN (P instanceof Object); True
}
}

In Java If there is an inheritance relationship, then the subclass instance instanceof the parent class also returns true
Copy Code code as follows:

Parent class
Class Person {
public String name;
public int age;
Person (String n, int a) {
name = name;
age = A;
}
}
Sub Class
public class Mans extends person{
Public String University;
Man (string n, int a, String s) {
Super (n, a);
University = s;
}
public static void Main (string[] args) {
Mans mm = new Man ("John resig", "PKU");
System.out.println (mm instanceof man); True
System.out.println (mm instanceof person); is True
}
}

Know these, JS in the following performance is not strange
Copy Code code as follows:

Define two constructors
function A () {}
function B () {}
A.prototype = B.prototype = {A:1};

Create an instance of two different constructors, respectively
var a = new A ();
var B = new B ();
Console.log (a instanceof B); True
Console.log (b instanceof A); True

We see that A and B are created with A and b respectively, but a instanceof B and B instanceof A are all true. That is, a is not created with constructor B, but still returns true. Because B.prototype exists on the internal prototype chain of a.

Because of the dynamic language characteristics of JS, you can modify the prototype at run time, so it is not surprising to return false below. The chain is interrupted because A.prototype is no longer in the internal prototype chain of a.

Copy Code code as follows:

function A () {}
var a = new A ();
A.prototype = {}; Dynamically modifying the prototype, note that you must create a
Console.log (a instanceof a); False

Note that writing also breaks the first of the above summary: Object obj was created through new constructor, then obj Instanceof constructor is true

Actually in the ECMAScript standard (whichever is 5.1), the internal implementation of instanceof calls the internal method of the constructor [[[Hasinstance]], described below

If f is a function object, when F (V) executes, the following steps occur:

1, if instanceof left operand V is not an object type, return false directly

Copy Code code as follows:

var A, B = 1, c = true, d = ' hello ';
Console.log (a instanceof Object); False here a value is undefined
Console.log (b instanceof Object); False
Console.log (c instanceof Object); False
Console.log (d instanceof Object); False

2/3, take constructor F prototype property, if not the object type, must throw TypeError exception,
Copy Code code as follows:

function A () {}
A.prototype = 1; The prototype of a is set to a non-object type
var a = new A ();
Console.log (a instanceof a);

Each browser throws a different exception hint,

Firefox18:

Chrome24:

Safari6:

OPERA12:

IE10:

  

4, the continuous implementation of the following logic: The V is set to the internal prototype of V, if V is null, return False, if both V and O point to the same object, return True.

Related Article

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.