Prototype and constructor in js

Source: Internet
Author: User

1. js only contains objects, including objects, functions, and constants. Objects do not need to be interpreted. Functions also have attributes. prototype is a common one. Constants also have attributes: (3 ). _ proto __; // Number {} 2. the prototype function of a function is a special object. It can directly execute its own code through parentheses. The function also has a special property prototype, which is also an object. The prototype object also has a special attribute constructor, which initially points to this function. That is, when js interprets the function keyword, it creates two objects: the function itself and the prototype object. At the same time, point the prototype attribute of the function object to prototype, and the constructor attribute of the prototype object to the function object. 3. the _ proto _ attribute of an object. Each object has a _ proto _ attribute (it may not be provided directly in earlier versions of IE). This attribute is very special, when calling an object's method or accessing the object's attributes, js will traverse the object itself in sequence, and the object's _ proto __, the _ proto _… of this object __...... That is, the key of prototype chain inheritance 4. The new Keyword new is used to create a method through a function. In fact, it mainly completes three tasks to create an object and calls the _ proto _ of this object to the prototype of the function. (Note that the function scope is the object created in the first step) finally, this object is returned. You can use the following code to replace function New (f, args) {var a ={};. _ proto _ = f. prototype; f. apply (a, args); return a;} 5. js Object diagram. xFunction is a custom function, xPrototype is the prototype of xFunction, and xObject = new xFunction () Object is the built-in Object of JS; _ prototype _ is the prototype of the Object. Empty is the _ proto __of all function objects. It is a special function without prototype. I hope you can discuss it and give some advice.

Related Article

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: 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.