JavaScript中的Functions returning function可能有什么用?
例如参考下面的代码
function a() {
alert('A!');
return function(){
alert('B!');
};
}
为了执行这两个功能,我们将调用 a()();
这样的代码构造有实际用途吗?忽略仅作为示例的alert()。
是的,有实际用途,例如bind
函数,用于几乎不指定执行上下文
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
完整的例子:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句