Function definitions can be nested in other functions, which are commonly used as subfunctions. But it cannot appear in loops or condition statements. Function quantity. It is applicable to functions that are only used once and do not need to be named. In the following example, although there is a fact function name, the latter is only used for self-calling.
The Code is as follows:
Var f = function (x)
{
Return x * x;
}
Var f = function fact (x)
{
If (x <= 1) return 1;
Else return x * fact (x-1 );
};
Parameter array of the function: Arguments object. Common arguments [I] References, arguments. length, etc.
Object:
Methods In object definition (function) are actually a function. The difference with nested functions is that object entities are referenced by the keyword "this.
The Code is as follows:
Function Rectangle (w, h)
{
This. width = w;
This. height = h;
This. area = area;
This. enlarge = Rectangle_enlarge;
This. setSize = setSize;
// Use the constructor to define a method
Function Rectangle_enlarge ()
{
This. width * = 2;
This. height * = 2;
}
Function setSize (width, height)
{
If (arguments. length <2)
{
Throw new Error ("arguments less! ");
}
Else if (arguments. length> = 2)
{
This. width = width;
This. height = height;
}
}
Function area ()
{
Return (this. width * this. height );
}
Function area1 ()
{
Alert (10 );
}
}
Prototype objects and inheritance:
A prototype object is an ideal place for storing methods and other common sense attributes, which is equivalent to a static field in C.