Function functions are the basis of JavaScript and a trigger point for implementing functions. Through example analysis, we will give you a deeper understanding of Function functions and explain their usage in practice.
Function is indeed an object. Any function we define is actually an instance of the Function object, and can also be understood as an instance of the Function object.
Since it is an instance of an object, it must point to a reference to the Function type. Since it points to a memory address of a reference type, you can also simply understand the function we defined as a variable, which points to an address of a reference type, which points to an instance of the Function object.
Since the function we define is actually a variable, the function instance address can point to multiple variables at the same time.
Look at the following code:
var add = new Function("n", "m", "return n + m");
The above is the standard function definition, and the constructor of the Function object is called. This constructor defaults to the N parameters of the previous function as parameters of the new function until the last parameter is considered to be the function body of the new function.
From the above statement, it is very intuitive to see that the variable add points to an instance of the Function type, but this naming method is very cumbersome and is equivalent to:
(1) Function expression
var add=function(n,m){ return n+m; }
(2) Function statement
function add(n,m){ return n+m; }
Since the declaration in the javascript language is advanced, the first edition advocates the use of the second method to define functions. Regarding the establishment of a separate article on the function declaration in advance, the
However, the first definition method is very intuitive to see that add is a variable pointing to a function instance.
Since it is a variable, it can be assigned to other variables, it can be passed as a parameter in the function or it can be returned from the function.
So var add2=add3=add; now all three variables point to references to this instance, now add=null; in the future, add2 and add3 functions can be used without being affected, because add removes the reference of the function object and points to the reference of null. Therefore, it does not affect the two functions add2 and add3 at all.
Therefore, functions can be passed in as parameters of other functions.
Therefore, the function can be returned as the return value of the function.
Because the function name is just a variable pointing to a function instance, there will be no overloading of functions in JavaScript, because the same variable points to the same reference address. The last one represents the same function.
Since a function is an instance of an object, it should have properties and methods. Therefore, functions in javascript have properties and methods.
4 more important attributes arguments , this , length , prototype
arguments represents the parameter class array of the current function. This property is very special. It also has a property called callee.
The property saves a pointer, which points to the function entity that owns this arguments property (that is equivalent to the function name)
This property is the current environment, similar to this in C#, indicating the current context
The length attribute indicates the maximum number of parameters received by the current function.
prototype represents the prototype of the function, that is, the method of the object instance is completely saved. In other words, all the methods on the prototype are inherited. For example, toString() valueOf(), etc.
Next, let's take a look at the function function types
Normal functions: introduce the characteristics of ordinary functions: overwrite the same name, arguments object, default return value, etc.
function ShowName(name) { alert(name); }
Anonymous functions: introduce the characteristics of anonymous functions: variable anonymous functions and nameless anonymous functions.
//Variable anonymous function, the left side can be variables, events, etc.var anonymousNormal = function (p1, p2) { alert(p1+p2); } anonymousNormal(3,6);//Output9
Closure function: introduces the characteristics of closure functions.
function funA() { var i = 0; function funB() { //Closure function funB i++; alert(i) } return funB; } var allShowA = funA(); //Global variable reference: cumulative output 1, 2, 3, 4, etc. function partShowA() { var showa = funA();// Local variable reference: only output 1 showa(); }