<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • JavaScript中的prototype.bind()方法介紹

    時間:2024-09-24 07:57:38 JavaScript 我要投稿
    • 相關推薦

    有關JavaScript中的prototype.bind()方法介紹

      以前,你可能會直接設置self=this或者that=this等等,這樣做當然也能起作用,但是使用Function.prototype.bind()會更好,看上去也更專業。

      下面舉個簡單的例子:

      復制代碼 代碼如下:

      var myObj = {

      specialFunction: function () {

      },

      anotherSpecialFunction: function () {

      },

      getAsyncData: function (cb) {

      cb();

      },

      render: function () {

      var that = this;

      this.getAsyncData(function () {

      that.specialFunction();

      that.anotherSpecialFunction();

      });

      }

      };

      myObj.render();

      在這個例子中,為了保持myObj上下文,設置了一個變量that=this,這樣是可行的,但是沒有使用Function.prototype.bind()看著更整潔:

      復制代碼 代碼如下:

      render: function () {

      this.getAsyncData(function () {

      this.specialFunction();

      this.anotherSpecialFunction();

      }.bind(this));

      }

      在調用.bind()時,它會簡單的創建一個新的函數,然后把this傳給這個函數。實現.bind()的代碼大概是這樣的:

      復制代碼 代碼如下:Function.prototype.bind = function (scope) {

      var fn = this;

      return function () {

      return fn.apply(scope);

      };

      }

      下面在看一個簡單的使用Function.prototype.bind()的例子:

      復制代碼 代碼如下:

      var foo = {

      x: 3

      };

      var bar = function(){

      console.log(this.x);

      };

      bar(); // undefined

      var boundFunc = bar.bind(foo);

      boundFunc(); // 3

      是不是很好用呢!不過遺憾的是IE8及以下的IE瀏覽器并不支持Function.prototype.bind()。支持的瀏覽器有Chrome 7+,Firefox 4.0+,IE 9+,Opera 11.60+,Safari 5.1.4+。雖然IE 8/7/6等瀏覽器不支持,但是Mozilla開發組為老版本的IE瀏覽器寫了一個功能類似的函數,代碼如下:

      復制代碼 代碼如下:

      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 () {

      return fToBind.apply(this instanceof fNOP && oThis

      ? this

      : oThis,

      aArgs.concat(Array.prototype.slice.call(arguments)));

      };

      fNOP.prototype = this.prototype;

      fBound.prototype = new fNOP();

      return fBound;

      };

      }

    【JavaScript中的prototype.bind()方法介紹】相關文章:

    JavaScript數組常用方法介紹09-04

    詳解JavaScript中的splice()使用方法08-20

    Javascript中arguments對象的詳解和使用方法08-20

    JavaScript常用方法匯總10-25

    javascript跨域訪問的方法07-09

    javascript編程異常處理的方法08-04

    JavaScript fontcolor方法入門實例07-07

    有關深入理解JavaScript中的并行處理的介紹10-14

    JavaScript中的with關鍵字07-24

    在Java中執行JavaScript代碼07-14

    主站蜘蛛池模板: 办公室久久精品| 亚洲日韩一页精品发布| 精品久久久无码中文字幕| 精品国产乱码一区二区三区| 欧美精品成人3d在线| 精品国产福利盛宴在线观看| 99热精品在线| 无码精品久久久天天影视| 精品精品国产理论在线观看| 国产精品自产拍在线观看| 精品久久人妻av中文字幕| 亚洲国产精品无码久久久蜜芽| 精品亚洲视频在线观看| 亚洲第一精品福利| 国产精品亚洲专区在线观看| 国产精品亚洲日韩欧美色窝窝色欲| 无码精品人妻一区二区三区漫画| 日韩精品一二三区| 久久伊人精品青青草原日本| 国产精品制服丝袜亚洲欧美| 亚洲人成亚洲精品| 亚洲精品国产成人专区| 日韩精品一区二区三区四区 | 精品国产第1页| 国产成人精品日本亚洲网址| 55夜色66夜色国产精品视频| 精品国产VA久久久久久久冰| 精品无码一区二区三区爱欲九九 | 免费精品视频在线| 日韩精品人成在线播放| 欧美日韩成人精品久久久免费看 | 99re66在线观看精品免费| 亚洲国产精品久久久久婷婷软件 | 精品无码久久久久久尤物| 久久久精品人妻一区二区三区蜜桃| 久久永久免费人妻精品下载| 久久99精品国产麻豆| 国产精品v片在线观看不卡| 97r久久精品国产99国产精| 久久精品国产只有精品2020| 午夜精品久视频在线观看|