鉴于此功能:
function foo( a,b,c ) {
//
}
Run Code Online (Sandbox Code Playgroud)
你怎么能做点什么,比如对每个参数使用console.log()?我知道你可以通过使用arguments关键字来查看参数.参数似乎返回看起来像Array的东西(但是类型为"object"[suprise .. not])但它不支持.forEach,我使用的是Chrome.
我尝试将函数修改为此函数,并期望它可以工作:
function foo( a,b,c ) {
arguments.forEach(function( arg ){
console.log( arg );
});
}
Run Code Online (Sandbox Code Playgroud)
您会收到TypeError:Object#没有方法'forEach'的错误.
该arguments对象没有forEach方法,因此您需要使用for循环或for...in循环迭代它.
?function foo () {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
foo("Foo", { "bar": 0 }); // Output: Foo, Object {bar: 0}
Run Code Online (Sandbox Code Playgroud)
要么
?function foo () {
for (var arg in arguments) {
console.log(arguments[arg]);
}
}
foo("Foo", "Bar"); // Output: Foo, Bar
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
152 次 |
| 最近记录: |