Analysis of function and object of "JavaScript" javascript

Source: Internet
Author: User

Objective:

JavaScript is object-oriented, and all objects have a prototype chain that belongs to them. object and function may be a lot of look at object instanceof function, function instanceof object is true and confusing, so first look at the object instance.

One, JS in the so-called example

1. such as var a = new A (); This is generally considered "A is an instance object of a function".

2. What is the process of the new operation?

1.new Create an empty object {} called small C

2. Then place the A.prototype at the top of the prototype chain of small c. That small c.__proto__ point to A.prototype

3. Execute a function, point A in this to small C, execute the end, if there is no return then the default return this reference.

Then one of the functions of new is to add A.prototype to the prototype chain of a.

3. Instanceof is the method of judging the example in JS, the basis of judging is (example a instanceof a) determine whether a prototype chain contains A.prototype.

4. An example of a general understanding of new is included in the synthesis. The so-called O is an instance of the FN object, actually refers to the O prototype chain contains Fn.prototype

Two, the fuzzy diagram of object and function (I draw very rubbing)

Explanation: The prototype property of all functions has constructor point to the function.

All objects have a prototype chain (Object.prototype's prototype chain is null).

__proto__ (in Chrome, FF) represents [[Proto]] built-in properties.

After seeing this figure, I should understand why instanceof is the result of that.

Third, the relationship between objects and object, prototype chain end in Object.prototype

The Object.prototype's prototype chain is empty (that is, there is no prototype object). Therefore Object.prototype instanceof Object is false.

Judging type with typeof, typeof (object.prototype) = = = ' object '. So not all objects are instances of object objects. All objects except Object.prototype should be instances of object objects.

I am a beginner welcome to look at the various suggestions, said the wrong place is thanks ...

Reprint Please specify source: http://blog.csdn.net/SyKent

http://blog.csdn.net/sykent/article/details/8003343

Analysis of function and object of "JavaScript" javascript

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.