The prototype and constructor in JS

Source: Internet
Author: User

The correctness of this paper has to be discussed, the Master passing please feel freeOnly objects in 1.js, including objects, functions, constants, and so on.

object is not explained. Functions also have attributes, one common is prototype. Constants also have properties:

(3). __proto__; // Number {}
2. Prototype of functions

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 is pointed at the beginning of the function.

That is, when JS is interpreted to the function keyword, two objects are created, one is the function itself, and the other is 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 an object

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

The key to the inheritance of the prototype chain

4.new keywords

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

In fact, it mainly completes three jobs

    1. Create an Object
    2. Point the __proto__ of the object to the prototype of the function
    3. Call this function (note that the scope of the function is the first step of the new object)

The object can be returned at the end. 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, no prototype,

Hope that we can discuss together, give guidance

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.