Function.prototype.testBind = function (scope) {
var fn = this; // this 指向的是调用testBind方法的一个函数
return function () { return fn.apply(scope, arguments);
}};
例子:
var
foo = {x: "Foo "};var
bar = function
(str) {
console.log(this.x+(arguments.length===0?'':str));};bar();
// undefinedvar
testBindBar = bar.testBind(foo); // 绑定 footestBindBar("Bar!");