函数重载必须依赖两件事情:判断传入参数数量的能力和判断传入参数的参数类型的能力css
1.判断传入参数数量的能力
js判断传入参数数量能够用arguments.length这个属性来判断;html
2.判断传入参数类型的能力
函数
js判断传入参数类型的方有2种:typeof和constructor;this
1.typeof
关于typeof的介绍能够查看:http://www.css88.com/article.asp?id=467
下面咱们使用type0f来判断对类型的一个例子:
spa
var num=“123″; var arr=“1,2,3,4″; if(typeof num==“string”) num = parseInt(num); alert(typeof num); if(typeof arr==“string”) arr = arr.split(“,”); alert(arr.length);
2.constructor
查看例子:
code
var num=“123″; var arr=“1,2,3,4″; if(num.constructor==String) num = parseInt(num); alert(typeof num); if(arr.constructor==String) arr = arr.split(“,”); alert(arr.length);