Function literals are suitable for functions that are used only once and do not need to be named. As in the following example, although the latter has a fact function name, it is only used for self-calling.
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 function: Arguments object. Commonly used arguments[i] references, arguments.length, etc.
Object:
The method in the object definition (function) is actually a function. The difference from the nested function is that the object entity is referenced through the keyword this.
function Rectangle(w, h)
{
this.width = w;
this.height = h;
this.area = area;
this.enlarge = Rectangle_enlarge;
this.setSize = setSize;
//Pass Constructor definition 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 :
The prototype object is an ideal place to store methods and other common attributes, which is equivalent to static fields in C#.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn