Home > Web Front-end > JS Tutorial > body text

JavaScript functions, methods, object codes_javascript skills

WBOY
Release: 2016-05-16 18:59:29
Original
984 people have browsed it

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.

Copy code 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 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.
Copy code 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;
//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#.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!