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