IE8环境下面可正常执行
if (!Function.bind) { // ie8下可正常执行
Function.prototype.bind = function (context) {
if (typeof this !== 'function') {
throw new Error('调用的对象不是方法');
}
var _this = this;
var result = function () {
return _this.apply(this instanceof result ? this : context);
}
var FN = function () { };
FN.prototype = this.prototype;
result.prototype = new FN();
return result;
}
}
//demo
var moduleTest = {
x: 81,
fn: function () {
return this.x;
}
};
var a = moduleTest.fn;
console.log(a());//指向window,undefined
var b = a.bind(moduleTest);//ie8下可正常执行
console.log(b());//改变指向,输出 81