Understanding Javascript_07 _ understanding the implementation principle of instanceof

Source: Internet
Author: User


So how is this kind of instanceof implemented? Now let's uncover the fog behind instanceof.

Instanceof Principle
By convention, let's look at a piece of code:
Copy codeThe Code is as follows:
Function Cat (){}
Cat. prototype = {}
Function Dog (){}
Dog. prototype = {}
Var dog1 = new Dog ();
Alert (dog1 instanceof Dog); // true
Alert (dog1 instanceof Object); // true
Dog. prototype = Cat. prototype;
Alert (dog1 instanceof Dog); // false
Alert (dog1 instanceof Cat); // false
Alert (dog1 instanceof Object); // true;
Var dog2 = new Dog ();
Alert (dog2 instanceof Dog); // true
Alert (dog2 instanceof Cat); // true
Alert (dog2 instanceof Object); // true
Dog. prototype = null;
Var dog3 = new Dog ();
Alert (dog3 instanceof Cat); // false
Alert (dog3 instanceof Object); // true
Alert (dog3 instanceof Dog); // error

Let's draw a memory Diagram for analysis:

The memory diagram is complex. Here is an explanation:
The program itself is a dynamic concept. With the execution of the program, Dog. prototype will change constantly. However, for convenience, I drew only one figure to express the changes of the three prototype references. In the heap, the right side is the memory representation of the function object, the center is the pointing of the prototype attribute of the function object, and the left side is the object instance created by the function object. The function object points to the pointer of the prototype attribute and writes dog1, dog2, and dog3 correspond to three reference changes of Dog. prototype respectively. They also have a relationship with dog1, dog2, and dog3 in the stack. (Note: Function objects will be explained in subsequent blog posts)
In dog3, if the prototype attribute of the function Object is null, the internal [[prototype] attribute of the function Object instance dog3 will point to the Object. prototype, which has been explained in "Understanding the creation process of Javascript_06 _ understanding objects.

Conclusion
According to the code running result and memory structure, the conclusion is as follows:
How instanceof checks whether object A is an instance of another object B is: Check whether the prototype of object B points to the object in the [[prototype] chain of object. If yes, true is returned. If not, false is returned. However, when prototype of object B is null, an error is returned (similar to a null pointer exception ).

Here we recommend an article from time to time, which is also about the principle of instanceof. It has different perspectives, but it is similar.

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.