Instanceof and class details in Javascript

Source: Internet
Author: User
{
Function onclick ()
{
This. style. display = 'none'; document. getelementbyid ('Code _ closed_text_120509 '). style. display = 'none'; document. getelementbyid ('Code _ open_image_120509 '). style. display = 'inline'; document. getelementbyid ('Code _ open_text_120509 '). style. display = 'inline ';
}
} "Id =" code_closed_image_120509 "> {
Function onclick ()
{
This. style. display = 'none'; document. getelementbyid ('Code _ open_text_120509 '). style. display = 'none'; getelementbyid ('Code _ closed_image_120509 '). style. display = 'inline'; getelementbyid ('Code _ closed_text_120509 '). style. display = 'inline ';
}
} "Id =" code_open_image_120509 "style =" display: none "> Code
How does instanceof work in JavaScript?
When JavaScript checks whether an object is an instance of a class, it is often obj. Instanceof Class
For example:
Function Class1 (){};
Function Class2 (){};
Class2.prototype = New Class1 ();
Function Class3 (){};
Class3.prototype = New Class2 ();
Function Class4 (){};
VaR OBJ = New Class3 ();
Alert (OBJ Instanceof Class3 ); // True
Alert (OBJ Instanceof Class2 ); // True
Alert (OBJ Instanceof Class1 ); // True

But have you ever wondered how the interpreter judges whether an object is a class instance? Most of the Internet is determined by the prototype chain.
We may see that class4 is not used, so add this sentence later.
Class2.prototype = New Class4 ();
Alert (OBJ Instanceof Class3 ); // True;
Alert (OBJ Instanceof Class2 ); // False;
Alert (OBJ Instanceof Class1 ); // True ;//

In JavaScript, each function has a prototype attribute, which is used to implement the inheritance mechanism. The function class1 defined below is required:
Function class1 (){}
Class1.prototype = {A: 10, B: 100 };
Each instance of class1 inherits the attributes a and B from prototype.

At the same time, each object will have an internal attribute _ PROTO _ (different JavaScript Virtual Machine implementations may use different names), which is invisible to JS developers, it is only used inside the VM. Every time an object is created, the _ PROTO _ of this object will be assigned the prototype of the constructor of this object, in this way, the _ PROTO _ attribute of the object references the same object as the prototype of the constructor. Once the object is created, the _ PROTO _ attribute will not change. In this way, A _ PROTO _ chain is formed through the _ PROTO _ attribute of the object and the _ PROTO _ attribute of the object referenced by _ PROTO. When accessing the attributes and methods of an object, the JS virtual machine is searched through the _ PROTO _ chain.

About instanceof:
Suppose there is a statement like this:
O instanceof C;
During the preceding statement execution, the virtual opportunity sets C. nodes on the _ PROTO _ chain of prototype and O are compared one by one. If an equal node is found, true is returned; otherwise, false is returned.

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.