还有一个答案则是 复制代码 代码如下: switch (arguments.length) { case 0: //Probably error break; case 1: //Do something break; case 2: default: //Fall through to handle case of more parameters //Do something else break; }
只是这种方式真的不好看,难道我们的函数最后要变成这样子的? 复制代码 代码如下: function zero1 (){ console.log("arguments 1") }; function zero2 (){ console.log("arguments 2") }; function zero () { if(arguments.length == 1){ zero1(); } else{ zero2(); } }
这里arguments的类型是一个对象,虽然数组的类型也是一个对象,虽然我们可以将之转换为一个数组 复制代码 代码如下: var args = Array.prototype.slice.call(arguments);
但是这也表明了这不是一个数组,它拥有的只有Array的唯一一个属性,即length。除此还有 arguments.callee Reference to the currently executing function. arguments.caller Reference to the function that invoked the currently executing function. arguments.length Reference to the number of arguments passed to the function.