Function.prototype.myBind = function (context = window, ...args1) {
const fn = this
return function(...args2) {
const allArgs = args1.concat(args2)
if (this instanceof(fn)){
return new fn(allArgs)
}
context.fn=fn
const result = context.fn(allArgs)
delete context.fn
return result
return fn.apply(context,allArgs)
}
}
Function.prototype.myApply = function(context=window,args){
console.log(context,"---------------传进来的实例")
console.log(this,"---------------this")
console.log(...args,"---------------args")
context.Symbol(fn)=this
const result = Array.isArray(args)?context.fn(...args):context.fn()
delete context.fn
return result
}
function a(name){
console.log(name,...arguments,"---------------------------a的log")
}
const obj={
a:'李四'
}
const b=a.myApply(obj,[1,2,3])
Function.prototype.myCall = function(context = window, ...args) {
if (this === Function.prototype) {
return undefined;
}
context = context || globalThis;
const fnKey = Symbol();
context[fnKey] = this;
const result = context[fnKey](...args);
delete context[fnKey];
return result;
};
function greet(name, greeting) {
console.log(`${greeting},${name}!`);
}
greet.myCall(null, 'Alice', 'Hello');