美文网首页web 杂谈WEB前端程序开发让前端飞
给 js MDN 提出关于 Function.prototype

给 js MDN 提出关于 Function.prototype

作者: 高少辉_骚辉 | 来源:发表于2017-08-05 14:39 被阅读107次

    MDN 原版

    if (!Function.prototype.bind) {
      Function.prototype.bind = function (oThis) {
          Function.prototype.bind = function (oThis) {
          if (typeof this !== "function") {
            // closest thing possible to the ECMAScript 5
            // internal IsCallable function
            throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")
          }
    
          var aArgs = Array.prototype.slice.call(arguments, 1), 
              fToBind = this, 
              fNOP = function () {},
              fBound = function () {
                
                return fToBind.apply(this instanceof fNOP
                                       ? this
                                       : oThis || this,
                                     aArgs.concat(Array.prototype.slice.call(arguments)))
              }
            fNOP.prototype = this.prototype
            fBound.prototype = new fNOP()
    
          return fBound
        }
    }
    

    提出修正的版本

    if (!Function.prototype.bind) {
      Function.prototype.bind = function (oThis) {
        if (typeof this !== "function") {
          // closest thing possible to the ECMAScript 5
          // internal IsCallable function
          throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")
        }
    
        var aArgs = Array.prototype.slice.call(arguments, 1), 
          fToBind = this, 
          fNOP = function () {},
          fBound = function () {
            fBound.prototype = this instanceof fNOP ? new fNOP() : fBound.prototype
            return fToBind.apply(this instanceof fNOP
                                     ? this
                                     : oThis || this,
                                     aArgs.concat(Array.prototype.slice.call(arguments)))
            }
        if( this.prototype ) {
          // Function.prototype doesn't have a prototype property
          fNOP.prototype = this.prototype
        }
    
        return fBound
      }
    }
    

    提出的修正地方有

    第一处

    fNOP.prototype = this.prototype 
                ⇩⇩⇩ 
    if( this.prototype ) {
       // Function.prototype doesn't have a prototype property
       fNOP.prototype = this.prototype
    }
    

    此处修改,主要是因为,函数 Function.prototype 是没有 prototype 原型的

    ps:可能有些人会奇怪 Function.prototype 不是对象吗?怎么变函数了,不信你可以去控制台打印出来看看

    第二处

    fBound.prototype = new fNOP()
                ⇩⇩⇩ 
    fBound.prototype = this instanceof fNOP ? new fNOP() : fBound.prototype
    

    此处修改是因为,原生 bind 函数是在没有使用 new 关键字的时候,是不会暴露出原始函数的 prototype 的

    所以我们此处,进行原型继承的时候,是在函数开始执行时候进行的,然后在函数内部判断,如果是使用 new 关键字,则继承,否则不暴露 prototype

    MDN Function.protype.bind 链接

    相关文章

      网友评论

        本文标题:给 js MDN 提出关于 Function.prototype

        本文链接:https://www.haomeiwen.com/subject/okoilxtx.html