js 实现apply

手写apply

/**
 * Function.apply(this: Function, thisArg: any, argArray?: any): any
 * @param {*} context 
 */
Function.prototype._apply = function(context){
    if(typeof this !== 'function'){
        throw 'Error'
    }
    context = context || window
    const args = [].concat(arguments[1] || [])
    context.fn = this
    const result = context.fn(...args)
    delete context.fn
    return result
}

const fn = function(){console.log(this);return {test:1,...this}}
fn._apply({_apply:1})
上一篇:JS 数组中指定位置插入另一个数组


下一篇:项目【官网】第六天--申请页面的编写