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;
      };
    }
  • 相关阅读:
    box-sizing
    max-width
    如何编写高质量CSS
    CSS文字大小单位PX、EM、PT
    jQuery设计思想
    pageX,clientX,offsetX,layerX的那些事
    html块级元素和内联元素区别详解
    centos彻底删除mysql
    删:[CentOS 7] 安装nginx
    CentOS7.0安装与配置Tomcat-7
  • 原文地址:https://www.cnblogs.com/xtreme/p/6834377.html
Copyright © 2011-2022 走看看