Functions are the basis for modular program design. To compile complex Ajax applications, you must have a deeper understanding of functions. Functions are the basis for modular program design. To compile complex Ajax applications, you must have a deeper understanding of functions. Functions in JavaScript are different from other languages. Each function is maintained and run as an object. Through the properties of function objects, you can easily assign a function to a variable or pass the function as a parameter. Before proceeding, let's take a look at the usage syntax of the function:
Function func1 (...) {...}
Var func2 = function (...) {...};
Var func3 = function func4 (...) {...};
Var func5 = new Function ();
These are the correct syntax for declaring functions. They differ greatly from common functions in other languages or previously described function definition methods. So why can this be written in JavaScript? What is the syntax it follows? The following describes the content.
Understanding Function objects)
You can use the function keyword to define a function, specify a function name for each function, and call it through the function name. During JavaScript interpretation and execution, functions are maintained as an Object, which is the Function Object to be introduced ).
Function objects are essentially different from other user-defined objects. Such objects are called internal objects, such as Date, Array, and String objects) all belong to internal objects. The constructor of these built-in objects is defined by JavaScript itself: an object is returned by executing a statement such as new Array (). JavaScript has a set of internal mechanisms to initialize the returned object, instead of specifying the object construction method by the user.
In JavaScript, Function objects correspond to the Function type. Just as Array objects correspond to the Array type and Date objects correspond to the Date type, you can use the new Function () create a function object. You can also use the function keyword to create an object. For ease of understanding, we compare the creation of function objects and the creation of array objects. First look at the array object: The following two lines of code are to create an array object myArray:
Var myArray = [];
// Equivalent
Var myArray = new Array ();
Similarly, the following two sections of code also create a function myFunction:
Function myFunction (a, B ){
Return a + B;
}
// Equivalent
Var myFunction = new Function ("a", "B", "return a + B ");
By comparing with the statements used to construct an array object, you can clearly see the essence of the function object. The function declaration described earlier is the first method of the above Code. Inside the interpreter, when this syntax is used, a Function object is automatically constructed to store and run the Function as an internal object. You can also see that a function object name (function variable) and a common variable name share the same specification and can reference this variable through the variable name, however, function variable names can be followed by brackets and parameter lists for function calling.
It is uncommon to create a Function in the form of new Function (), because a Function body usually has multiple statements. If they are passed as parameters in the form of a string, the code is less readable. The following describes the Syntax:
Var funcName = new Function (p1, p2, pn, body );
The parameter types are all strings. p1 to pn indicates the list of parameter names of the created function, body indicates the function body statement of the created function, and funcName indicates the name of the created function. You can create an empty function without specifying any parameters, and create an unknown function without specifying funcName. Of course, such a function does not make any sense.
It should be noted that p1 to pn is a list of parameter names, that is, p1 can represent not only a parameter, but also a list of parameters separated by commas, for example, the following definition is equivalent:
New Function ("a", "B", "c", "return a + B + c ")
New Function ("a, B, c", "return a + B + c ")
New Function ("a, B", "c", "return a + B + c ")
JavaScript introduces the Function type and provides the syntax of new Function () Because Function objects must be of the Function type when adding attributes and methods.
The essence of a function is an internal object, which is determined by the JavaScript interpreter. Functions created using the code above can be called using the function name in the program. The function definition problems listed at the beginning of this section are also explained. Note that you can add parentheses after the function declaration to call the function immediately after the creation is complete. For example:
Var I = function (a, B ){
Return a + B;
} (1, 2 );
Alert (I );
This code will show that the value of variable I is equal to 3. I indicates the returned value, rather than the created function, because the brackets "(" has a higher priority than the equal sign "=. Such Code may not be commonly used, but it is a good solution when users want to implement modular design in a long code segment or want to avoid name conflicts.
It should be noted that, although the following two methods for creating functions are equivalent:
Function funcName (){
// Function body
}
// Equivalent
Var funcName = function (){
// Function body
}
However, the previous method is to create a famous function, while the latter is to create an unknown function, just to point a variable to this unknown function. There is only one difference in usage: For a famous function, it can be defined after it is called. For an unknown function, it must be defined before it is called. For example:
<Script language = "JavaScript" type = "text/javascript">
<! --
Func ();
Var func = function (){
Alert (1)
}
// -->
</Script>
This statement will generate an undefined error in func, and:
<Script language = "JavaScript" type = "text/javascript">
<! --
Func ();
Function func (){
Alert (1)
}
// -->
</Script>
The statement can be correctly executed, and the following statement can also be correctly executed:
<Script language = "JavaScript" type = "text/javascript">
<! --
Func ();
Var someFunc = function func (){
Alert (1)
}
// -->
</Script>
It can be seen that although JavaScript is an interpreted language, it will check whether the corresponding function definition exists in the entire code During function calling. This function name only uses function funcName () the format defined is valid, but not an anonymous function.
Relationship Between Function objects and other internal objects
In addition to function objects, there are also many internal objects, such as Object, Array, Date, RegExp, Math, and Error. These names actually represent a type, and an object can be returned through the new operator. However, function objects are different from other objects. When typeof is used to obtain the type of a function object, it still returns the string "function", while typeof is an array object or another object, it returns the string "object ". The following code demonstrates different types of typeof:
Alert (typeof (Function )));
Alert (typeof (new Function ()));
Alert (typeof (Array ));
Alert (typeof (Object ));
Alert (typeof (new Array ()));
Alert (typeof (new Date ()));
Alert (typeof (new Object ()));
After running this code, we can find that all the first four statements show "function", and the last three statements show "object". It can be seen that a new function actually returns a function. This is quite different from other objects. Other types of arrays and objects will return a common Object through the new operator. Although a function is also an object, it is different from a common object because it is also an object constructor. That is to say, you can use a new function to return an object, this is already described earlier. All typeof objects that return "function" are function objects. This is also called constructor. Therefore, all constructors are objects, but not all objects are constructors.
Since functions are also an object, their types are functions, and the class definitions of C ++, Java, and other object-oriented languages can be inferred to the role of function types, that is, some methods and attributes can be defined for the Function object itself. With the help of the prototype object of the Function, you can easily modify and expand the definition of the Function type. For example, the following extends the Function type Function, the method1 method is added for it. The pop-up dialog box displays "function ":
Function. prototype. method1 = function (){
Alert ("function ");
}
Function func1 (a, B, c ){
Return a + B + c;
}
Func1.method1 ();
Func1.method1. method1 ();
Note the last statement: func1.method1. mehotd1 (), which calls the method1 method of the method1 function object. Although it may seem confusing, the syntax is clear: this is a recursive definition. Because method1 is also a Function, it also has the attributes and methods of Function objects. All method extensions of Function types have such recursive properties.
Function is the basis of all Function objects, while Object is the basis of all objects (including Function objects. In JavaScript, any Object is an Object instance. Therefore, you can modify the Object type to make all objects have some common attributes and methods, you can modify the Object type through prototype:
Object. prototype. getType = function (){
Return typeof (this );
}
Var array1 = new Array ();
Function func1 (a, B ){
Return a + B;
}
Alert (array1.getType ());
Alert (func1.getType ());
The above Code adds the getType method to all objects to return the object type. The two alert statements show "object" and "function" respectively ".
Passing functions as parameters
We have already introduced the essence of function objects. Each function is represented as a special object, which can be conveniently assigned to a variable and then called by the variable name. As a variable, it can be passed to another function in the form of parameters. This usage has been seen in the JavaScript event processing mechanism described earlier, for example, the following program passes func1 as a parameter to func2:
Function func1 (theFunc ){
TheFunc ();
}
Function func2 (){
Alert ("OK ");
}
Func1 (func2 );
In the last statement, func2 is passed to theFunc as an object, and then theFunc is called internally by func1. In fact, passing functions as parameters or assigning functions to other variables is the basis of all event mechanisms.
For example, if you need to perform initialization during page loading, You can first define an init initialization function and then use window. onload = init; the statement binds it to the event loaded on the page. Init is a function object, which can be added to the onload event list of window.
The above is an in-depth understanding of functions in JavaScript. For more articles, please refer to the PHP Chinese website (www.php1.cn )!