【发布时间】:2018-07-31 05:43:42
【问题描述】:
我有一些正在运行的代码,但我想将其抽象为一个功能组件,我可以在脚本的其他地方使用它。我收到一个未定义的错误:
这行得通:
//add an index to each element
var items = learning_check.map(function(el,i) {
var o = Object.assign({}, el);
o.key = i;
return o;
});
这不是:
const addIndex = (a) => {
console.log('addIndex initiated')
a.map = (el,i) => {
var o = Object.assign({}, el);
o.key = i;
return o;
}
}
调用
const mItems = addIndex(learning_check); // initiated
console.log('mItems: ' + mItems); // undefined
【问题讨论】:
-
你不是
returningmap的结果 -addIndex没有return声明。 -
在你的第二个代码块中,你不是在调用 map,而是在覆盖它。
标签: javascript ecmascript-6 arrow-functions