JavaScript Functions, methods, and object code

Source: Internet
Author: User

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.
Copy codeThe 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.
Copy codeThe 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.

Related Article

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.