JavaScript functions, methods, object code _javascript Tips

Source: Internet
Author: User
The direct amount of the function, which is useful for functions that are used only once and need not be named. The following example, which has the fact function name, is used only as a self invocation.
Copy Code code 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);
};

An array of arguments for the function: arguments object. Common Arguments[i] references, arguments.length, etc.
Object:
The method in the object definition (function), which is also a function, differs from the nested function in that it refers to the object entity through the keyword this.
Copy Code code as follows:

function Rectangle (W, h)
{
This.width = W;
This.height = h;
This.area = area;
This.enlarge = Rectangle_enlarge;
This.setsize = setSize;
Defining methods by constructors
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 to store methods and other common-sense attributes, equivalent to static fields in C #.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.