JavaScript基礎之--- 手寫 apply方法 的實現


 

手寫實現如下:

 

Function.prototype.myApply = fucntion(context) {
    if(typeof this !== 'function') {
        throw new TypeError('not a function!')
    }
    context = context || window
    context.fn = this
    let result
    if(arguments[1]) {
        result = context.fn(...arguments[1])
    }else{
        result = context.fn()
    }
    delete context.fn
    return result
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM