var name = 'global name'
const club = {
name: 'Hello World',
getName: function(a, b, c) {
console.log(this.name, a, b, c);
},
getName2: () => {
console.log(this.name);
}
}
Function.prototype.myCall = function() {
const context = arguments[0];
const argsArray = [];
for (let i = 1; i < arguments.length; i++) {
argsArray.push(arguments[i]);
}
const fn = this;
context.fn = fn;
context.fn(...argsArray);
delete context.fn;
}
Function.prototype.myApply = function() {
const context = arguments[0];
const argsArray = arguments[1];
const fn = this;
context.fn = fn;
context.fn(...argsArray);
delete context.fn;
}
Function.prototype.myBind = function() {
const context = [].shift.call(arguments);
const fn = this;
const argsArray = arguments;
return function() {
fn.call(context, ...argsArray, ...arguments);
};
}
const test = { name: 'test' }
const getNameBinded = club.getName.myBind(test, 1, 2);
getNameBinded(3);
console