使用带有回调函数的函数表达式,实现了箭头函数

Eng*_*ion 0 javascript binding callback

当我使用箭头函数作为回调函数时,我在下面的代码工作得很好

var getNumber = function (argument, callback) {   
callback(argument - 1);
} 
getNumber(10, (x)=>{
console.log(x); // x = 9
});
Run Code Online (Sandbox Code Playgroud)

现在当我想在下面的代码中将箭头函数更改为函数表达式时.

var getNumber = function (argument, callback) {
callback(argument - 1);
}
getNumber(10, action(x)); // x is not defined 
function action(x){
console.log(x);
}
Run Code Online (Sandbox Code Playgroud)

遗憾的是我得到错误说x没有定义.

Dun*_*ker 5

在你的第二个片段中,你没有传递函数,你正在调用函数,然后将结果作为参数传递.你要

getNumber(10, action); // x is not defined 
function action(x){
    console.log(x);
}
Run Code Online (Sandbox Code Playgroud)