Understanding javascript_10 _ Object Model

Source: Internet
Author: User
Tags types of functions

Object Model

The red dotted line indicates the implicit prototype chain.
This object model diagram contains too many things that need to be carefully understood in many places. You can write some test code for verification. After a thorough understanding of this figure, we can almost understand the Javascript language. Below are some additional instructions:
1. build-in function constructor is mentioned in the figure in several places. This is the same object and can be tested and verified: CopyCode The Code is as follows: // passed in ff2.0, IE7, opera9.25, safari3.0.4
Function = function. constructor // result: True
Function = function. Prototype. constructor // result: True
Function = object. constructor // result: True
// Function also equals to number. constructor, String. constructor, array. constructor, Regexp. constructor, etc.
Function FN (){}
Function = fn. constructor // result: True

This illustrates several problems: the function points to the built-in function constructor of the system; The function is self-initiated; all the functions in the system are constructed by the function.

2. obj1, obj2... objn refers to the objects created using code like this: function fn1 () {}; var obj1 = new fn1 (); these objects do not have the local constructor method, however, they will get an inherited constructor method from the prototype chain, that is, fn. prototype. constructor can be seen from the construction process of function objects. It is FN itself.

3. obj1, obj2... objn refers to the object created with code like this: var obj1 = new object (); var obj1 = {}; or var obj1 = new number (123 ); or obj1 =/\ W +. Therefore, the prototype links of these objects and the constructor values inherited from the prototype chain (whether their constructor is build-in number constructor or build-in object constructor) depends on the specific object type. In addition, note that VaR OBJ = new object (123); the type of the created object is still number, that is, it needs to be determined based on the type of the parameter value. Similarly, they do not have a local constructor, but instead obtain the inherited constructor method from the prototype chain, that is, build-in *** constructor, which is determined by the data type.
Sample Code

Copy code The Code is as follows: // custom object representation, corresponding to use defined functions in JavaScript Object Model
Function Foo (){}
// Representative of the object instance created by the custom object, corresponding to objects that created by user defined functions in the JavaScript Object Model
VaR Foo = new Foo ();
// String built-in function representative
// STR represents the object instance created by built-in functions, corresponding to objects that created by build-in constructors in JavaScript Object Model
VaR STR = new string ("string ");

Memory display

You will find that it is the same as the memory analysis graph in "Understanding javascript_09_function and object". Why? As mentioned in data model, all built-in objects can be considered as derived types of functions. For example, if number instanceof function is true, number instanceof object is true. In this sense, they can be treated like user-defined functions. Therefore, the process for creating built-in and custom objects is the same.

This blog post is based on understanding function and object. Therefore, you must understand the relationship between function and object!

Finally, let's write a speech: crazy theory!

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.