Prototype and constructor in JS

Source: Internet
Author: User
only objects in the 1.js, including objects, functions, constants, and so on.

Object without explanation. Functions also have properties, one of the common is prototype. Constants also have attributes:

(3). __proto__;//number {}
2. Prototype of function

A function is a special object that can execute its own code directly through parentheses.

The function also has a special property prototype, which is also an object.

The prototype object also has a special property constructor, which points to the function at the beginning.

That is, when JS interprets the function keyword, it creates two objects, one for the function itself and one for the prototype object. The prototype property of the function object is also pointed to prototype, and the constructor property of the prototype object points to the function object.

3. __proto__ Properties of Objects

Each object has a __proto__ attribute (which may not be directly given in a lower version of IE), which is very special, because when calling the object's method or accessing the object's properties, JS iterates through the object itself, the object's __proto__, the object's __proto__ __ Proto__ ...

Which is the key to the prototype chain inheritance.

4.new Keywords

The new keyword is used to create a method from a function.

In fact, it basically completes three jobs

    1. Create an Object
    2. To point the object's __proto__ to the prototype of the function
    3. Call this function (at this point, note that the scope of the function is the first step of the new object)

Finally, you can return the object. You can replace it with the following code

function New (F,args) {var a= {};a.__proto__ = f.prototype;f.apply (A,args); return A;}
5.js Object diagram

Where: Xfunction is a custom function, Xprototype is the prototype of Xfunction, Xobject=new xfunction ()

object is JS built-in Object;_prototype_ is the prototype of object

Empty is the __proto__ of all function objects, is a special function, without prototype,



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.