Analysis of constructor and prototype attributes of JS Functions

Source: Internet
Author: User

The function is a magical child shoe in JS. So how to create a function? In simple terms, there are several forms:
1. Definition
Function (){}
2. Declarative
Var a = function () {}; // assign an anonymous function to a variable
3. Constructor
Var a = new Function (); // use Function to instantiate an object

What is the purpose of a function? Encapsulate and reuse, right? encapsulate some actions so that other shoes can call the [use the () operator] repeatedly ].
Functions can be divided:
1. Common functions;
Common functions are the encapsulation and reuse of general methods, as shown in a above;
2. constructor;
The constructor is used to instantiate an object, such as a recipe. A dish is stir-fried according to this recipe. To define the constructor, an unwritten rule is to write a function name, for example: var A = function (){};
3. Object methods;
Methods used to define an object. For example, a Person object var Person ={} needs to define a getName method, such as: Person. getName = function (){};

Function constructor
Each function has a constructor attribute that points to the creation of its own function. For example, in function a above, who is his constructor pointing? Who created it and directed it? Who created it? Enter a. constructor on the firebug console to display Function (). Why is Function? In fact, no matter which form is used to create a Function, it will be created in the background in the form of new Function. Who is the constructor of Function pointing? Unexpectedly, is it a Function? Why? Because Function is a local object specified by ECMAScript and is independent from the host environment, this indicates that the Function can be used only when the host environment (such as a browser) is opened.

Prototype of Function
Like constructor, prototype is the default attribute of each function. This attribute points to the prototype object, and prototype actually has a constructor attribute pointing to this function.

Constructor
The constructor can instantiate an object, for example, var Person = function () {}; var p = new Person (); this p is created through Person, then its constrctor points to Person, and there is a _ proto _ attribute inside it. You can directly access this attribute in firefox, chrome, and other browsers. This attribute points to the prototype object.

Relationships between instances, prototypes, and constructors
The instance is created through the constructor, but it has no direct relationship with the constructor. The _ proto _ attribute inside the constructor directs to the constructor, the prototype attribute of the constructor points to the prototype. Therefore, both the instance and the constructor have a relationship through prototype ....

Author: "Feng shangshi's blog"

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.