Function objects in JavascriptYes. functions are used in three ways.Class:
As common logicCodeContainer;
As an object method;
As a constructor.
1. As a general logic code container
Function multiply (X, Y){
Return x * Y;
}
The multiply function encapsulates two-digit multiplication operations.Calculation formula:
VaR Product = multiply (128,128 ); // Product = 16384
You can create a function instance in three ways. The first type is declarative, that is, like declarativeLike a variable, an anonymous function created using the function () {} identifier is directly assigned to the variable.Function name used for calling:
VaR multiply = function (x, y ){
Return x * Y;
}
The second is the definition, that is, using the function keyword followed by the function name and () {} to directly define the name function. The first multiply function is created through the definition.
The third type is the constructor, that is, the constructor function is called through the new operator to create a function.This method is rarely used, soI will not introduce it.
Among the three methods for creating a function, there are still slight differences between declarative and defined types. For example, the function in the following Code adopts the declarative method:
VaR example = function (){
Return 1;
}
Example ();
VaR example = function (){
Return 2;
}
Example ();
The execution result is as follows:
1
2
If the definition is used, that is:
Function example (){
Return 1;
}
Example ();
Function example (){
Return 2;
}
Example ();
You will get another result:
2
2
That is, when a function with the same name is created using the definitionThe created function will overwrite the previously created function.. ThisThe difference is caused by the working mechanism of the Javascript interpretation engine.Javascript InterpretationBefore the engine executes any function callFirstRegisters a function created in a defined manner in the global scope, and then executes the function call in sequence. Because function RegistrationThe later-defined function overrides the first-defined function. Therefore, no matter where the call statement is located, the latter-defined function is executed. On the contrary, for declarative functions, the Javascript interpretation engine will treat any declared variable like it will wait until the execution of the callThe variable is evaluated only when the Code of this variable is used.Because JavaScript code is executed sequentially from top to bottom, when the first example () call is executed, The code of the example function is to first define the codeWhen the second example () call is executed, the code of the example function becomes the Code defined later.
2. As an object Method
When parsing code, JavaScript will declare or define a letterNumber of specified call objects. The called object is the execution environment of the function. If a function contains a variable declared with the keyword "this", this references the called object.
In fact, there are also call objects in common functions, but this call object is the default global window object. For example:
VaR Product = Window. Multiply (128,128); // Product = 16384
This indicates that by default, the call object of the function defined or declared in the global scope is window.
In object-oriented programming, functions that act as object members are usually called methods. For example:
VaR dog = {};
Dog. Name= "Heibao ";
Dog. Age = "3 months ";
Dog. Shout = function (){
Return "Hello, my name is" + this. Name + "and I am" + this. Age + "old! ";
}
Dog. Shout (); // "Hello, my name is heibao and I am 3 months old !"
Interestingly, objects can also be borrowed from other objects.Method:
VaR cat = {};
Cat. Name= "Xiaohua ";
Cat. Age = "2 years ";
Cat. Greet = dog. Shout;
Cat. Greet (); // "Hello, my name is Xiaohua and I am 2 years old !"
In addition, use the call and apply methods of function objects.,You can also dynamically specify the call object of a function or method.:
Dog. Shout. Call(Cat );// "HeLlo, my name is Xiaohua and I am 2 years old !"
Or
Dog. Shout. Apply (CAT); // "Hello, my name is Xiaohua and I am 2 years old !"
3. As a constructor
Javascript simulates classes in object-oriented languages through constructors. For example:
Function animal (sort, character ){
This. Sort = sort;
This. Character= Character;
}
Using animal as the constructor, you can create a new object as follows:
VaR dog = new animal ("Mammal", "Four Legs ");
The process of creating a dog object is as follows: first, the new operator creates an empty object ({})Then, call the function animal with this empty object as the calling object, and add two attributes sort and character for this empty object,Then, set the default constructor of the empty object.Modify the attribute to the name of the constructor.(That is, animal; the default constructor attribute value is object when an empty object is created), and set the _ PROTO _ attribute of the empty object to point to animal. prototype -- this is the so-called object initialization. Finally, returnThe object after initialization. The returned new object is assigned to the variable dog.
Dog. Sort; // Mammal
Dog. character; // Four legs
Dog. constructor; // animal
Smart readers may combine the content described aboveThe new operation will be consideredThe operator can call constructors to create objects.As follows:
VaR dog = {};
Animal. Call (dog, "Mammal", "Four Legs ");
On the surface, the two lines of code correspond to VaR dog = new animal ("Mammal", "Four Legs"); is equivalent, but not actually. Although the execution environment of the specified functionPartial object initialization can be achieved, such as empty object dogThe sort and character attributes are obtained:
Dog. Sort; // Mammal
Dog. character; // Four legs
Dog. constructor; // object -- Note: The default constructor attribute of the dog object is not modified.
However, the most important thing is the newly created dog.The object cannot inherit other objects through the animal. Prototype attribute.As long as you compare it with the previous process of using the new operator to call the constructor to create an object, you will find that during the initialization of the new object, in addition to adding explicitly declared attributes to the new object, A "dark box operation" will also be performed on the new object -- rewrite the constructor attribute of the new object to animal, and set the _ PROTO _ attribute of the new object to point to animal. prototype. Although you can manually "initialize the object", you can also rewrite dog. constructor to animal, but according to ecma262. The _ PROTO _ attribute of an object is read-only to developers. The setting of this attribute can only be completed by the Javascript interpretation engine when an object is created using the new operator..
Javascript is based on prototype inheritance. If the _ PR of an object cannot be set correctlyOto _ ownerThis means that the default Inheritance Mechanism will become invalid:
Animal. Prototype. Greet = "Hi, Good lucky! ";
Dog. Greet; // undefined
In fact, in Firefox, The __proto _ attribute is also writable:
Animal. Prototype. Greet = "Hi, Good lucky! ";
Dog. _ PROTO _ = animal. Prototype;
Dog. Greet; // hi, good lucky!
But this can only work in Firefox. Considering compatibility with multiple browsers, prototype-based inheritance can be realized only by relying on the new operator.
Reproduced in: http://blog.sina.com.cn/s/blog_81adceb001012n51.html