1. Functions
The definition of a function in the mass-user is this: a function is an event-driven or reusable block of code that executes when it is invoked.
Admittedly, we don't get anything of value from this abstract definition. Here are some examples of how a function can be defined:
function Add (NUM1, num2) {
return NUM1 + num2;} var add = function (NUM1, num2) { return num1 + num2;} This is the more common two kinds of
The following two species are relatively rare
var add=new Function ("Num1", "num2", "return num1+num2");
var add=function ("Num1", "num2", "return num1+num2");
The above four methods are the correct syntax for creating a function. However, the common is generally the first two types. Because there are some flaws in the latter two compared to the first two types.
- The latter two kinds of comparison cumbersome and not intuitive. This can be seen in the above example.
- There are some fatal flaws in the latter two. This function is created in such a way that it is not possible to maintain a chain of scopes that are part of a function, and the functions created at any time under the new function are equivalent to those created under the global scope. This can be demonstrated in the following example.
var x = 1; var add = (function () { var x = +; return function (num) { return num + x; }} ()); Console.log (Add (0));//100
var x = 1; var add = (function () { var x = +; return new Function (' Num1 ', ' return num1+x ');} ()); Console.log (Add (0));//1
In other words, the latter two ways of creating a function cannot form a complete chain of function scopes (which will be discussed later), nor can there be a so-called closure.
- The latter two are too inefficient to operate.
First of all, JS is not efficient for parsing strings, and there are a lot of strings in the (new) function.
Secondly, JS interpreter, for use function () {}, this form of functions, have some form of optimization. Like the following.
var array = [
];
for (var i = 0; i <; i++) {
Array[i] = function () {
return ' undefined ';
}
}//the first Kind
var array = [
];
for (var i = 0; i <; i++) {
Array[i] = new Function ("return undefined");
}//the second kind,
there is a big gap between these two approaches in operational efficiency. For, the first one only needs to execute once function () {}, the other 999 times is an assignment, and then a function to execute 1000 times to create and assign a value.
It is because of the three reasons on three sides that the function () {} is more prevalent in this way.
In addition, you may have seen the following, but this form is only a variant.
var New function () { return 1 + 2;}; Console.log (typeof add);//objectvar result = add.constructor ();/* The call must be made in this way */console.log (result);//3
In this form, the new function () is created essentially using an anonymous function to create an object. A constructor property of this object just points to its constructor, which is the anonymous function. So it's actually an ugly way of writing.
Here, we are just describing a few ways to define a function. By comparison, we know the first two are more practical, but even so, there is a huge difference between the first and second definitions. In the next section, we'll go on to the difference between the two ways.
JavaScript series----functions (function) Chapter