有人可以将此箭头功能转换为普通功能吗?

时间:2019-04-29 03:28:30

标签: javascript web ecmascript-6 arrow-functions

我无法将此箭头功能转换为普通功能。我已经在Chrome的控制台面板中对此进行了测试。该代码摘自Es6课程中的freecodeCamp.org

//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];

4 个答案:

答案 0 :(得分:4)

箭头函数中"use strict"; var realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]; var squareList = function squareList(arr) { "use strict"; var squaredIntegers = arr.filter(function (num) { return Number.isInteger(num) && num > 0; }); return squaredIntegers; }; var squaredIntegers = squareList(realNumberArray); console.log(squaredIntegers); 之后的任何表达式都将隐式返回函数,但是您需要使用=>关键字在普通函数中显式返回。

return

答案 1 :(得分:1)

我将您的代码插入Babel中并得到了这个信息:

List_of_entries = []

def create_entries(r):
    global List_of_entries
    for i in List_of_entries:
        i.destroy()
    List_of_entries = []
    for i in range(r):
        entry = tk.Entry(frame)
        entry.grid(row=i,column=0,padx=2)
        List_of_entries.append(entry)

(通常,当您需要将ES6 +语法转换为ES5时,可以使用Babel自动执行。)

答案 2 :(得分:1)

  <span style={{ marginLeft: '6px'}} onClick= 
         {()=>this.clickLike(this.props.like)}>
       <a href="#" >Like</a>   
   </span>

我想这是音乐作品。

答案 3 :(得分:0)

您在那里遇到了一些问题。

函数语法为

function name(param_list)
function name(p1,pn)

也许最好保留内联声明。

function natural(num)
{
   return Number.isInteger(num) && num > 0;
}

function squarelist(arr)
{
    "use strict";
    const squaredIntegers = arr.filter(natural); //notice non lambda needs a name
    return squaredIntegers;
}