Function.prototype.myBind = function() {
let fn = this
let ags = [...arguments].slice(1)
let context = arguments[0]
return function () {
let params = [...ags, ...arguments]
return fn.apply(context, params)
}
}
var a = 10;
var test = function(a, b) {
console.log(this.a);
}
test()
var bindTest = test.myBind({a: "111"}, 'aaa')
bindTest('bbb');
Function.prototype.myApply = function(context) {
let ags = [...arguments].slice(1)
context = context || window
context.fn = this
context.fn(...ags)
delete context.fn
}
var a = 10;
var test = function(a) {
console.log('---------a: ', a);
console.log(this.a);
}
test()
test.myApply({a: "111"}, ['aaa', 'bbb'])
Function.prototype.myCall = function(context) {
let ags = [...arguments].slice(1)
context = context || window
context.fn = this
context.fn(...ags)
delete context.fn
}
var a = 10;
var test = function(a, b) {
console.log('---------a, b: ', a, b);
console.log(this.a);
}
test()
test.myCall({a: "111"}, 'aaa', 'bbb')