[英]Why I should use anonymous function to stored my function as variable
why this code run perfectly为什么这段代码运行完美
function remember(number) {
return function() {
return number;
}
}
const returnedFunction = remember(5);
console.log( returnedFunction() );
// 5
but this code give me error但是这段代码给了我错误
function remember(number) {
return number;
}
const returnedFunction = remember(5);
console.log( returnedFunction() );
// returnedFunction is not a function
In the first example, your function remember
returns another function, which you then you can execute.在第一个示例中,您的 function
remember
返回另一个 function,然后您可以执行它。 In the second example, your function remember
returns a number.在第二个示例中,您的 function
remember
返回一个数字。 That's why returnedFunction()
runs fine in the first case, but throws an error in the second.这就是为什么
returnedFunction()
在第一种情况下运行良好,但在第二种情况下抛出错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.