虽然尝试阅读“javascript忍者的 secret ”(好吧,所以也许我还没有资格阅读这本书),但我看到下面的代码,我明白代码在做什么,但部分我真的不明白是调用函数(索引)的地方。索引是任意术语还是某种 JavaScript 来指示通用索引?
<script type="text/javascript">
function forEach(list,callback) {
for (var n = 0; n < list.length; n++) {
callback.call(list[n],n);
}
}
var weapons = ['shuriken','katana','nunchucks'];
forEach(
weapons,
function(index){
function
 assert(this == weapons [index],
"Got the expected value of " + weapons [index]);
} );
</script>
最佳答案
它作为第二个参数传递给 forEach
。
forEach(list,callback)
…到一个名为callback
的变量。
所以在这里调用:
callback.call(list[n],n);
通过 call method
Is index arbitrary terms or some sort of javascript to indicate generic index?
这是一个参数名称。您自己定义了该名称。它所传递的值是在您调用该函数时确定的。
关于javascript - javascript 函数中的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43760775/