zoukankan      html  css  js  c++  java
  • 手写bind

    1.普通版

    if(Function.prototype.bind === undefined){

      console.log('您的浏览器不支持bind方法,使用手写bind功能')

      Function.prototype.bind = function(obj){

        var arg1 = [].slice.call(arguments,1)

        var fun = this

        return function(){

        fun.apply(obj,arg1.concat([].slice.call(arguments,1)))

        }

      }

    }else{

      console.log('您的浏览器支持bind方法')

    }

    2. MDN标准源码

    if(!Function.prototype.bind) {

      Function.prototype.bind = function(oThis) {

        if(typeof this !== 'function') {

          throw new TypeError('Error')

        }

      var args = Array.prototype.slice.call(arguments,1)

      var fToBind = this

      var fNOP = function(){}

      var fBound = function() {

        return fToBind.apply(this instanceof fNOP && oThis ? this : oThis || window,

                  args.concat(Array.prototype.slice.call(arguments)))

      }

      fNOP.prototype = this.prototype

      fBound.prototype = new fNOP()

      return fBound

      }

    }

    ————学习记录

  • 相关阅读:
    linux基础知识-17
    linux基础知识-16
    linux基础知识-15
    linux基础知识-14
    linux基础知识-13
    phone 调试三种工具
    ANT_HOME is set incorrectly or ant could not be located .Please set ANT_HOME.
    如何解决google ping不通的问题。
    weinre targets none 的问题
    phonegap3.5了结
  • 原文地址:https://www.cnblogs.com/atao24/p/13329798.html
Copyright © 2011-2022 走看看