Overview
function is the basis of modular programming, writing complex AJAX applications, you must have a more in-depth understanding of the function.
Functions in JavaScript are different from other languages, and each function is maintained and run as an object. By the nature of the function object, it is convenient to assign a function to a variable or to pass the function as a parameter. Before continuing, take a look at the use syntax of the function:
function func1(…){…}
var func2=function(…){…};
var func3=function func4(…){…};
var func5=new Function();
These are the correct syntax for declaring a function. They are very different from functions that are common in other languages, or functions that are previously described. So why do you write that in JavaScript? What is the syntax it follows? These are described below.
Understanding function Objects (Functions object)
You can define a function by using the function keyword, and specify a function name for each function, which is invoked by the name of the functions. When JavaScript interprets execution, the function is maintained as an object, which is the function object to introduce.
function objects are essentially different from other users ' defined objects, which are called internal objects, such as date objects (dates), array objects (array), string objects (strings), which are internal objects. The constructors for these built-in objects are defined by JavaScript itself: returning an object by executing a statement such as the new Array (), where JavaScript has a mechanism to initialize the returned object rather than the user specifying how the object is constructed.
In JavaScript, the type of the function object corresponds to functions, just as the type of the array object is the array, and the Date object corresponds to the type date, and you can create a function object by using the new function (). You can also create an object by using the function keyword. For ease of understanding, we compare the creation of function objects and the creation of arrays of objects. First look at the array object: The following two lines of code are to create an array object myarray:
var myArray=[];
//等价于
var myArray=new Array();
同样,下面的两段代码也都是创建一个函数myFunction:
function myFunction(a,b){
return a+b;
}
//等价于
var myFunction=new Function("a","b","return a+b");
By comparing and constructing the Array object statement, you can clearly see the nature of the function object, the function declaration described earlier is the first way of the above code, but inside the interpreter, when this syntax is encountered, a function object is automatically constructed and stored and run as an internal object. As you can see here, a Function object name (function variable) and a normal variable name have the same specification, can be referenced by variable name to this variable, but the function variable name can be followed by parentheses and argument list for function calls.
Creating a function in the form of the new function () is uncommon because a function body usually has multiple statements that, if passed as a string, are not readable by the code. Here's how to use the syntax:
var funcname=new Function (p1,p2,..., pn,body);
The type of the parameter is a string, p1 to the list of parameter names for the function created by the PN, and the body represents the function statement of the function being created, FuncName is the name of the function being created. You can create an empty function without specifying any parameters, and do not specify funcname to create a nameless function, of course, that function has no meaning.
Note that P1 to PN is a list of parameter names, that is, P1 not only represents an argument, it can also be a comma-separated list of arguments, such as 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")