In-depth understanding of functions in JavaScript

xiaoxiao2021-04-01  232

If you infringe your interests, please let us know.

The overview function is the basis for modular programming, writing complex Ajax applications, must have a more in-depth understanding of functions. The functions in JavaScript are different from other languages, and each function is maintained and running as an object. Through the nature of the function object, it is easy to assign a function to a variable or transfer the function as a parameter. Before continuing to tell, look at the function of the function: function func1 (...) {...} var func2 = function (...) {...}; var func3 = function func4 (...) {...}; var func5 = new function ( These are the correct syntax of the declaration function. They have a great difference in their functionality in other languages ​​or the function definitions previously described. So why you can write in JavaScript? What is the grammar it follow? These content will be described below. The Function Object can define a function with the function keyword and specify a function name for each function, and call through the function name. When JavaScript explains execution, the function is maintained as an object, which is the function object to be introduced. Function objects have essentially distinctive objects defined by other users, which are called internal objects, such as Date Objects (DATE), array objects (Array), string objects (String) belong to internal objects. The constructor of these built-in objects is defined by JavaScript itself: Returns an object by executing new array (), and a mechanism has a set of mechanisms to initialize the returned object, not by the user to specify the structure of the object. In JavaScript, the type corresponding to the function object is function. For the type corresponding to the array object is Array. The type corresponding to the date object is DATE. You can create a function object through new function (), or create the function keyword. An object. For ease of understanding, we compare the creation of the function object and the creation of an array object. First see array objects: The following two lines of code are created an array object myarray: var myarray = []; // is equivalent to var myarray = new array (); the same, the following two codes are also created a function MyFunction : Function myfunction (A, b) {RETURN A B;} // Isometric Var myfunction = new function ("a", "b", "return A b"); comparison of array object statements It can be clearly seen that the function object is nature, the function declaration of the previous introduction is the first way of the above code, and inside the interpreter, when this syntax is encountered, a Function object is automatically constructed, and the function is used as one The internal object is stored and running. From here, you can also see that a function object name (function variable) and a normal variable name have the same specification, all can reference this variable through the variable name, but the function variable name can follow the brackets and parameter lists to function. transfer. Create a function in the form of new function () is not common because a function body usually has multiple statements, and if they use them as a parameter transmission, the readability is poor.

Here is the syntax of it: var funcname = new function (P1, P2, ..., PN, BODY); the type of parameter is a string, and the P1 to PN represents the parameter name list of the created function, and Body represents the creation. The function of the function, FUNCNAME is the name of the creation function. You may not specify any parameters to create an empty function, not specifying FUNCNAME to create an unknown function, of course, the function is not any meaning. It should be noted that P1 to PN is a list of parameter names, that is, P1 can not only represent a parameter, but it can also be a comma-separated parameter list, such as the definition below 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 a syntax for new function () because the function object adds attributes and methods to be used by Function. The essence of functions is an internal object that determines its operation mode by the JavaScript interpreter. The function created by the code described above can be called using the function name in the program. The function definition issues listed at the beginning of this section also have been released. Note that the function call can be done directly after the function declaration is added to the creation completion, for example: var i = function (a, b) {RETURN A B;} (1, 2); Alert (i); The segment code will display the value of the variable I equal to 3. i is a value that is returned, not a function created, because parentheses "(" "Compare the equal sign" = "has a higher priority. Such code may not use, but when users want to be in a long code segment Multiple design or avoid naming conflicts, this is a good solution. Need not to note that although the following two creating functions are equivalent: function funcname () {// Function} // Equivalent The VAR FUNCNAME = function () {// function body} But the previous way is a famous function, and the subsequent creation has a nameless function, just let a variable point to this unknown function. Only one difference between use Yes: For the famous function, it can appear again after the call; for the nameless function, it must be defined before the call.

For example: