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

标签 javascript arrays callback functional-programming

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/

相关文章:

javascript - 我无法让这个回调工作

javascript - casperjs 无法打开 https url

javascript - 如何在等待响应时在网站中呈现加载页面

c - 使用指针而不是索引进行排序

objective-c - Objective-C 方法的 C 数组参数和返回类型

Android IntentService 将数据返回给 Activity

javascript - 更改 JSON 时无法读取未定义的属性 'appendChild'

javascript - 使用他们的 API 构建维基百科查看器,总是出现错误

python - 将 NumPy 数组列表写入 csv

haskell - 在 Haskell 中存储多态回调