JS Method overloading

Source: Internet
Author: User

JS Method overloading

Like what

function fun1 (obj) {alert (1)}
function fun1 (obj, obj1, obj2) {alert (3)}
function Fun1 (obj2,obj3) {alert (2)}
Fun1 ();

The last Popup is 2. Because it's covered!

Generally do this:

    functionFUN1 (obj) {alert (1) }    functionFun3 (obj, obj1, obj2) {alert (3) }    functionFun2 (obj, obj1) {alert (2) }      functionfunall (obj, obj1, Obj2, obj3) {if(Arguments.length = = 1) {fun1 (obj); }        Else if(Arguments.length = = 2) {fun2 (obj, obj1); }        Else if(Arguments.length = = 3) {fun3 (obj, obj1, obj2); }        //code here to show the meaning of overloading        //We Can do Something ...} funall (""); Funall ("", ""); Funall ("", "","");


However, the function is the internal object function of JS, which uses the internal arguments. Monitoring can see that the Function has the Arguments property, thecaller property.

See also: http://www.jb51.net/article/43987.htm

When calling a function, the arguments inside the parentheses can be different from the defined number, which can be obtained by arguments, for example:

<input type= "button" onclick= "Fun (1)"/>
<script>
function Fun () {
if (arguments.length===1) {
Alert (arguments[0]);//1
}
}

Again, say:

<input type= "button" onclick= "Fun ()"/>
<script>
function Fun (obj) {
if (arguments.length = = = 1) {
Alert (arguments[0]);
} else {
Alert (0); 0
}
}

Referring to the function object, see http://www.w3school.com.cn/js/pro_js_functions_function_object.asp

Like what:

The first parameter of the new Function is the parameter name, and the second argument is the code block.

var doadd = new Function ("INum", "alert (INum + 20)");
var doadd = new Function ("INum", "alert (INum + 10)");
Doadd (10);

This is the advanced learning of JS. See W3school inside ECMAScript study.

JS Method overloading

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.