zoukankan      html  css  js  c++  java
  • javascript对象bind()方法兼容处理

    bind() 函数在 ECMA-262 第五版才被加入;它可能无法在所有浏览器上运行。你可以部份地在脚本开头加入以下代码,就能使它运作,让不支持的浏览器也能使用 bind() 功能

    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, // 此处的 this 指向目标函数
            fNOP = function() {},
            fBound = function() {
              return fToBind.apply(this instanceof fNOP
                ? this // 此处 this 为 调用 new obj() 时所生成的 obj 本身
                : oThis || this, // 若 oThis 无效则将 fBound 绑定到 this
                // 将通过 bind 传递的参数和调用时传递的参数进行合并, 并作为最终的参数传递
                aArgs.concat(Array.prototype.slice.call(arguments)));
            };
    
        // 将目标函数的原型对象拷贝到新函数中,因为目标函数有可能被当作构造函数使用
        fNOP.prototype = this.prototype;
        fBound.prototype = new fNOP();
    
        return fBound;
      };
    }
  • 相关阅读:
    redis修改密码
    redis配置
    django中日志配置
    django中缓存配置
    navicat批量导入数据
    django添加REST_FRAMEWORK 接口浏览
    django验证码配置与使用
    LUA_OBJECT
    LUA comment
    lua-redis
  • 原文地址:https://www.cnblogs.com/xtreme/p/6834377.html
Copyright © 2011-2022 走看看