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

      }

    }

    ————学习记录

  • 相关阅读:
    Vasya and Endless Credits CodeForces
    Dreamoon and Strings CodeForces
    Online Meeting CodeForces
    数塔取数 基础dp
    1001 数组中和等于K的数对 1090 3个数和为0
    1091 线段的重叠
    51nod 最小周长
    走格子 51nod
    1289 大鱼吃小鱼
    POJ 1979 Red and Black
  • 原文地址:https://www.cnblogs.com/atao24/p/13329798.html
Copyright © 2011-2022 走看看