模拟实现javaScript call函数

0
function dragon(name){
    this.name = name
}

function say(content){
    console.log(this.name + ':' + content)
}

Function.prototype.myCall = function(params){
    params = params || window //多场景适配,当没有传入参数或传入参数为undefined、null的时,指定为window

    params.fn = this //绑定this,代替上下文执行

    let ars = [] //创建空数组保存形参

    for(let i = 1; i < arguments.length; i++){ // 从第1位开始循环,将形参存入空数组(第0位是传入的函数实例)
        ars.push(arguments[i])
    }
    //let ars = Array.prototype.slice(arguments,1)
    let result = params.fn(...ars) //执行上下文函数,并获取返回结果

    delete params.fn //删除上下文函数
    return result
}

var kk = new dragon('CXK')

say.myCall(kk,'大家好,我是练习时长两年半的偶像练习生!')

var nums = [1,23,435,345,34,45,3,8,25,677,349,563]

var maxNum = Math.max.myCall({},...nums)
// var maxNum = Math.max.call(Math,...nums)
console.log(maxNum)