function makeIncrementer() {
return function(x) { return x + 1; };
}
var inc = makeIncrementer();
// displays 8
alert(inc(7));明明inc没参数,后面的x怎么理解
return function(x) { return x + 1; };
}
var inc = makeIncrementer();
// displays 8
alert(inc(7));明明inc没参数,后面的x怎么理解
实际上返回function(x) { return x + 1; }返回这个函数又有什么用呢?还是不明白得很。
这个相当于var inc = function(x) { return x + 1; };
inc(7);
返回8,应该很清楚了。
这句话会返回一个匿名函数的句柄(引用),
var inc = makeIncrementer();
这样赋值以后,inc 就获得匿名函数的句柄(引用),也可以理解为 inc 就是匿名函数的函数名了,
inc 加上括号 () 就可以执行匿名函数了。js 可以这么用,可是 lz 的例子显得很多于,直接调用就可以了!
function increase(x) {
return x + 1;
}
// displays 8
alert(increase(7));