SOURCE

Function.prototype.customCall=function(context,...args){
    if(context==null) context=globalThis
    if(typeof context !=='object') context=new Object(context)//值类型
 
    const fnKey=Symbol()//不会出现属性名的覆盖
    context[fnKey]=this//this就是当前对象

    const res=context[fnKey](...args)//绑定了this

    delete context[fnKey]//清理fn,防止污染
    return res
}




//------------------------------------------apply,传入数组
Function.prototype.customApply=function(context,args){

    if(context==null) context=globalThis
    if(typeof context !=='object'){
        context=new Object(context)
    }

    const fnKey=Symbol()
    context[fnKey]=this

    const res=context[fnKey](...args)//绑定this

    delete context[fnKey]
    return this

}

function fn(a,b,c){
    console.log(a+b+c)
}

fn.customApply({x:100},[10,20,30])
console 命令行工具 X clear

                    
>
console