为什么将匿名函数传递给map
函数可以工作,但尝试传递函数表达式会引发错误?
arr = [2,4,6,8];
items = arr.map(function(x) {
return Math.pow(x, 2);
});
console.log(items); // Returns [4, 16, 36, 64]
squareIt = function(x) {
return Math.pow(x, 2);
}
otherItems = arr.map(squareIt(x));
console.log(otherItems); // Returns "Uncaught ReferenceError: x is not defined"