bind 方法实现

【要求】:实现 bind 方法

【实现】:

// 简单方法
Function.prototype.bind = Function.prototpe.bind || function(context) {
var me = this;

return function() {
return me.apply(context, arguments);
}
}

// 考虑柯里化的情况,更加健壮的 bind()
Function.prototype.bind = function(context) {
var args = Array.prototype.slice.call(arguments, 1),
me = this;

return function() {
var innerArgs = Array.prototype.slice.call(arguments),
finalArgs = args.concat(innerArgs);

return me.apply(context, finalArgs);
}
}

☂ 参考:Javascript中bind()方法的使用与实现


posted on 2016-09-17 22:16  Ruth92  阅读(168)  评论(0)    收藏  举报

导航