bind polyfill
JavaScript
var obj1 = {
name: 'obj1',
method1: function() {
console.log(this.name);
}
};
var obj2 = {
name: 'obj2',
method2: function() {
console.log(this.name);
}
};
Function.prototype.bind = function(context) {
var self = this;
return function() {
return self.apply(context);
}
};
var method2 = obj1.method1.bind(obj2);
var method1 = obj2.method2.bind(obj1);
console.log(method1());
console.log(method2());