Old*_*zer 6 javascript functional-programming
In http://eloquentjavascript.net/1st_edition/chapter6.html, there is the following example:
function negate(func) {
return function(x) {
return !func(x);
};
}
var isNotNaN = negate(isNaN);
alert(isNotNaN(NaN));
Run Code Online (Sandbox Code Playgroud)
Knowing only basic Javascript and imperative programming, I am stumped by this programming style. Can someone help me to understand what happens during runtime.
我逐步完成代码并检查变量,发现值x是NaN.它是如何知道isNaN应该作为x匿名函数的参数传递的参数?首先,为什么实际参数NaN isNotNaN成为参数isNaN(即,当isNaN期望一个参数时,为什么它从参数中获取isNotNaN)?
理解这一点的最好方法可能是看看这些东西实际上是相等的。注意如何func变成传递的isNaN函数。
function negate(func) {
return function(x) {
return !func(x);
};
}
var isNotNaN = negate(isNaN);
/*
isNotNaN = function(x){
return !isNaN(x)
}
*/
alert(isNotNaN(NaN));
Run Code Online (Sandbox Code Playgroud)