Understanding and practice of javascript Function, javascriptfunction
Function functions are the foundation of javascript and an exciting point for implementing functions. We use instance analysis to give you a deeper understanding of Function functions and explain their usage in practice.
A Function is indeed an object, and any Function we define is actually an instance of a Function object. It can also be understood as an instance pointing to a Function object.
Since it is an instance of an object, it must point to a reference of the Function type. since it points to a memory address of the reference type, you can simply think of our defined function as a variable. This variable points to an address of the reference type, this address points to an instance of the Function object.
Since the defined function is actually a variable, the instance address of this function can point to multiple variables at the same time.
See the following code:
Var add = new Function ("n", "m", "return n + m ");
The above is a standard Function definition and calls the Function object constructor. This constructor defaults the previous N parameters to the new Function parameters, until the last parameter is considered as the function body of the new function.
From the preceding statement, we can intuitively see that the add variable points to a Function-type instance, but this naming method is very cumbersome and is equivalent:
(1) function expressions
var add=function(n,m){return n+m;}
(2) function declaration
function add(n,m){return n+m;}
Because the statement in javascript language is in advance, the first version advocates using the second method to define the function.
However, the first definition method intuitively shows that add is a variable pointing to a function instance.
Since it is a variable, you can assign values to other variables, which can be passed as parameters in the function or returned from the function.
So var add2 = add3 = add; now all the three variables point to the reference of this instance. Now add = null; in the future, add2 and add3 functions can be used completely without being affected, because add removes the reference of the function object and points to the null reference. therefore, the add2 and add3 functions are not affected 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 only a variable pointing to the function instance, the function in javascript will not be overloaded, because the same variable points to the same reference address, and the last expression is the same function.
Since a function is an object instance, there should be attributes and Methods. Therefore, functions in javascript have attributes and methods.
Important four attributes: arguments, this, length, prototype
Arguments indicates the array of parameter classes of the current function. This attribute is special. It also has a property called callee,
The arguments. callee attribute saves a pointer pointing to the function entity that owns the arguments attribute (equivalent to the function name)
This attribute 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 indicates the prototype of the function, that is, the method of the object instance is completely saved. In other words, the methods on the prototype are inherited, such as toString () valueOf.
Next, let's take a look at the function types.
Common functions: describes the features of common functions: overwrite the same name, arguments object, and default return value.
function ShowName(name) { alert(name);}
Anonymous functions: Introduce the features of anonymous functions: Variable anonymous functions and non-name anonymous functions.
// Variable anonymous function. The left side can be variables, events, and other var anonymousNormal = function (p1, p2) {alert (p1 + p2);} anonymousNormal (3, 6); // output 9
Closure Function: describes the features of the closure function.
Function funA () {var I = 0; function funB () {// closure function funB I ++; alert (I)} return funB;} var allShowA = funA (); // global variable reference: accumulative output of functions such as 1, 2, 4, and 3, partShowA () {var showa = funA (); // local variable reference: Only output 1 showa ();}