Function.prototype.myCall = function (context) {
const ctx = context || window
const fn = Symbol('fn');
ctx[fn] = this;
const [first, ...args] = arguments;
ctx[fn](...args);
delete ctx[fn]
}
Function.prototype.myApply = function (context, args) {
const ctx = context || window
const fn = Symbol('fn');
ctx[fn] = this;
ctx[fn](...args);
delete ctx[fn]
}
Function.prototype.myBind = function (context) {
const fn = this;
const [first, ...args] = arguments;
return function () {
fn.call(context, ...args)
}
}
var obj = {
home: '北京'
}
function test(name, age) {
console.log('名字:', name, '年龄:', age, '住址:', this.home)
}
test.apply(obj, ['小明', 18])
const fn = test.myBind(obj, '小明', 18);
fn(18)
console