1. The function is actually an object, and the function name is actually a pointer to the function object.
function sum (num1,num2) {return
num1+num2
}
Alert (sum (10,10));//20
var anothersum=sum;
Alert (Anothersum (10,10));//20
sum=null;
Alert (Anothersum (10,10));//20
So there is no overload, a function with the same name that overrides the former. 2. Function declarations and function expressions
Normal Operation
alert (SUM (10,10));
function sum (num1,num2) {return
num1+num2
}
Error
alert (sum (10,10)) appears;
var sum=function (num1,num2) {return
num1+num2;
};
3. Function as a value
You can return another function from one function, such as the following example:
function Compare (PropertyName) {return
function (obj1,obj2) {
var value1=obj1[propertyname];
var value2=obj2[propertyname];
return value2-value1;}
4. Function Internal Properties
There are two special objects inside the function: arguments and this. Arguments contains all the arguments for the passed-in function, which has a callee property, which is a pointer to the function that owns the arguments object.
function FAC (num) {
if (num<=1) return
1;
else return
NUM*FAC (num-1);
}
This is a typical factorial function that uses a function name inside its function, which increases the degree of coupling between execution and name, so it can be as follows:
function FAC (num) {
if (num<=1) return
1;
else return
Num*arguments.callee (num-1);
}
var Anotherfac=fac;
Alert (ANOTHERFAC (5));//120
fac=function () {return
0;
}
Alert (ANOTHERFAC (5));//120
This refers to the environment object to which the function is executed.
Window.color= "Red";
function Saycolor () {
alert (this.color);
}
var o={
color: "Blue"
}
Saycolor ();//red
O.saycolor=saycolor;
O.saycolor ();//blue
There is also an attribute: caller. It holds a reference to the function that called the current function.
Arguments.callee and Arguments.caller cannot be used in strict mode.
5. Function Method
There are two methods: Call () and apply (). The purpose is to run the function in a specific scope. Apply accepts two parameters: one is the scope of the run function and the other is an array of parameters.
function sum (num1,num2) {return
num1+num2
}
function sum2 (num1,num2) {return
sum.apply (this,[num1,num2]);
}
function sum3 (num1,num2) {return
sum.apply (this,arguments);
}
Alert (sum2 (10,10));//20
alert (sum3 (10,10));//20
The difference between call () is that its parameters must be enumerated individually.
function Sum4 (num1,num2) {return
sum.apply (this,num1,num2);
}
Alert (SUM4 (10,10));//20
The powerful thing about these two methods is that they can enlarge the scope on which the function is run.
Window.color= "Red";
var o={color: "Blue"};
function Saycolor () {
alert (this.color);
}
Saycolor ();//red
saycolor.apply (this);//red
saycolor.apply (o);//blue
Obviously the advantage of expanding the scope is that the object does not need to have any coupling relationship with the method.
The function inherits the ToString () and toLocaleString () and valueof () to return the code for the function.