Object-oriented JavaScript (note) _js object-oriented

Source: Internet
Author: User
First, the reference
Copy Code code as follows:

Produces an Array object
var items = new Array (' 1 ', ' 2 ', ' 3 ');
Causes a reference to point to the object
var itemref = items;
Items.push (' 4 ');
Items and Itemref point to the same object
Alert (Items.length = = itemref.length);

Modifying an object produces a new object
var item = ' Test ';
var itemref = Item;
item+= ' ing ';
Item and Itemref no longer point to the same object at this time
Alert (item!= itemref);

Second, to determine the number and type of incoming parameters
Copy Code code as follows:

Arguments can be used to determine the number of function parameters
Function SendMessage (msg,obj) {
if (Arguments.length ==2)
Obj.handlemsg (msg);
Else
Alert (msg);
}

The decision type can use the constructor property of the TypeOf and JavaScript objects
Copy Code code as follows:

typeof can use a string to express the type name of a variable
Determines whether a variable num is a string type
if (typeof num = ' string ')
But typeof pairs are both object array types that cannot be distinguished

Use constructor to interpret whether Num is a string type
if (Num.constructor = = String)
if (Num.constructor = = Array)

This function determines the length and variable type of a function's variable
function Strict (Types,args) {
if (types.length!= args.length) {
Throw "Invalid number of arguments";
}
for (var i=0; i<args.length; i++) {
if (Args[i].constructor!= types[i]) {
Throw ' parameter type mismatch '
}
}
}

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.