【发布时间】:2019-04-29 03:28:29
【问题描述】:
我无法将此箭头函数转换为普通函数。我已经在 chrome 的控制台面板中对此进行了测试。此代码取自 freecodeCamp.org 的 Es6 课程
//This is what I have tried. The final output result is showing undefined
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = function(arr) {
"use strict";
const squaredIntegers = function(num) {
(function() {
arr.filter(Number.isInteger(num) && num > 0);
});
return squaredIntegers;
}
}
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
//Here is the Arrow function I was trying to convert
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
"use strict";
const squaredIntegers = arr.filter(num => Number.isInteger(num) && num > 0);
return squaredIntegers;
};
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
//The code should output this
[4, 42, 6];
【问题讨论】:
-
只用函数关键字替换语法
-
你做的外层没问题...那你为什么不能用内层复制你所做的呢?我的意思是,看起来你知道自己在做什么
标签: javascript web ecmascript-6 arrow-functions