Introduction to arguments and overloading in JavaScript _javascript tips

Source: Internet
Author: User

Because of the error in language design, arguments can be treated as an array.

Copy Code code as follows:

function zero () {
Console.log (Arguments[0]);
}

There will also be
Copy Code code as follows:

function zero () {
for (Var i=0;i<arguments.length;i++) {
Console.log (Arguments[i]);
}
}

It leverages the fact that JavaScript is JAVASC

The arguments variable here provides an array-like interface to the argument. Because of the variable parameters of the arguments here, we can use this interesting thing to do some interesting things, such as overloading.

Javscript overload

There's a question about overloading on the stackvoerflow, so there's the first answer.

Copy Code code as follows:

if (typeof friend = = "undefined") {

} else {

}

And then there's one answer.

Copy Code code as follows:

Switch (arguments.length) {
Case 0:
Probably Error
Break
Case 1:
Do something
Break
Case 2:
Default://fall through to handle case of the more parameters
Do something else
Break
}

Just this way is really not good to see, is our function finally to become such a child?

Copy Code code as follows:

function Zero1 () {
Console.log (' arguments 1 ')
};
function Zero2 () {
Console.log (' Arguments 2 ')
};
function zero () {
if (arguments.length = = 1) {
Zero1 ();
} else{
Zero2 ();
}
}

It's really hard to see, even if we switch. Case, also not look ah.

Javascript arguments is not an array

Arguments is not always an array as we see it, sometimes it may not be.

Copy Code code as follows:

function Hello () {
Console.log (typeof arguments);
}

The type of arguments here is an object, although the type of the array is also an object, although we can convert it to an array
Copy Code code as follows:

var args = Array.prototype.slice.call (arguments);

But it also shows that this is not an array, it has only one property of array, that is, length. In addition to this, there are

Arguments.callee

Reference to the currently executing function.

Arguments.caller

Reference to the function of that invoked the currently executing function.

Arguments.length

Reference to the number of arguments passed to the function.

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.