gpt4 book ai didi

javascript - 匿名回调函数抛出错误,使得像forEach一样遍历数组

转载 作者:行者123 更新时间:2023-11-27 23:16:17 24 4
gpt4 key购买 nike

function each(collec,function(){
for(var key in collec){
console.log(collec[key]);
}
}
);

------------------------------------------
Line 1: Expected an identifier and instead saw 'function' (a reserved word).
Line 1: Expected ')' to match '(' from line 1 and instead saw '('.
Line 1: 'function closure expressions' is only available in Mozilla JavaScript extensions (use moz option).
Line 1: Expected an identifier and instead saw ')'.
Line 6: Expected an identifier and instead saw ')'.
Line 6: Expected an assignment or function call and instead saw an expression.

最佳答案

我们在调用函数时传递参数,但这里

function each(collec,function(){
for(var key in collec){
console.log(collec[key]);
}
}
);

您正在声明一个函数,因此需要指定参数,如下所示

function each(collec , temp){
.... fxn code
}

What is the difference between arguments and parameters in javascript?

关于javascript - 匿名回调函数抛出错误,使得像forEach一样遍历数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35779941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com