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"