JavaScript object-oriented Tutorial: Object and function

Source: Internet
Author: User
Tags object contains empty object object

Article Introduction: JavaScript is object-oriented, and all objects have a prototype chain of their own. object and function may look at the object instanceof function, the function instanceof object are all true, so first look at the instance of the objects.

JavaScript is object-oriented, and all objects have a prototype chain of their own. object and function may look at the object instanceof function, the function instanceof object are all true, so first look at the instance of the objects.

One, JS in the so-called example

1. such as var a = new A (); It is generally considered that "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. Small c.__proto__ Pointing A.prototype

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

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

3. instanceof for JS to judge the example of the method, the basis of judgment is (example a instanceof a) to determine whether a prototype chain contains A.prototype.

4. The general knowledge of new examples is included. The so-called O is the example object of FN, actually refers to the O prototype chain contains Fn.prototype

Two, object and function of the fuzzy diagram (oneself draw very rub)

Explanation: All the prototype objects of the functions have constructor, prototype properties. All objects have a prototype chain (Object.prototype's prototype chain is null)

After reading this picture, you should understand why instanceof is the result of that.

The relationship between objects and object, the prototype chain ends in Object.prototype

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

Judge 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 object.



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.