<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      從babel編譯結果分析class的實現原理

      Posted on 2021-09-15 14:57  過鹿人  閱讀(195)  評論(0)    收藏  舉報

       

      示例:

      class A {
          // 屬性表達式
          prop1 = 1;
          // get方法
          get value() {
            console.log('Getting the current value!');
            return this.prop1;
          }
          // set方法
          set value(newValue) {
            console.log('Setting the current value!');
            this.prop1 = newValue;
          }
          // 箭頭函數表達式
          arrowFunc = (...args) => {
              console.log(args);
          }
          // constructor
          constructor(b = 2) {
              this.prop2 = b;
          }
          // 普通函數表達式
          Func() {
              console.log("Func");
          }
          // 靜態屬性
          static prop3 = 3;
          // 靜態普通函數
          static staticFunc() {
              console.log("staticFunc", this);
          }
          // 靜態箭頭函數
          static staticArrowFunc = () => {
              console.log("staticArrowFunc", this);
          }
      }
      const a = new A(3);

      使用babel編譯成es5的代碼,編譯結果會生成幾個內部函數:

      // 類的調用檢查,如果調用A不使用new,則會報錯
      function _classCallCheck(instance, Constructor) {
        if (!(instance instanceof Constructor)) {
          throw new TypeError("Cannot call a class as a function");
        }
      }
      // 屬性描述符默認配置為不可枚舉
      function _defineProperties(target, props) {
        for (var i = 0; i < props.length; i++) {
          var descriptor = props[i];
          descriptor.enumerable = descriptor.enumerable || false;
          descriptor.configurable = true;
          if ("value" in descriptor) descriptor.writable = true;
          Object.defineProperty(target, descriptor.key, descriptor);
        }
      }
      // 定義原型上的普通函數、靜態普通函數、get和set方法
      function _createClass(Constructor, protoProps, staticProps) {
        if (protoProps) _defineProperties(Constructor.prototype, protoProps);
        if (staticProps) _defineProperties(Constructor, staticProps);
        return Constructor;
      }
      // 定義屬性到obj上,重新賦值使用defineProperty,第一次賦值直接添加屬性
      function _defineProperty(obj, key, value) {
        if (key in obj) {
          Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
          });
        } else {
          obj[key] = value;
        }
        return obj;
      }

      類的編譯結果:

      // A是一個立即執行函數 返回結果是個函數 函數名就是類名
      var A = /*#__PURE__*/ (function () {
        // constructor
        function A() {
          // 轉化constructor中的默認參數
          var b =
            arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
          // 檢查類是否使用new執行
          _classCallCheck(this, A);
          // 屬性表達式定義到類實例上
          _defineProperty(this, "prop1", 1);
          // 箭頭函數表達式定義到類實例上
          _defineProperty(this, "arrowFunc", function () {
            // 這個for循環是轉換...args,遍歷arguments,將每一項賦值給args對象
            for (
              var _len = arguments.length, args = new Array(_len), _key = 0;
              _key < _len;
              _key++
            ) {
              args[_key] = arguments[_key];
            }
            console.log(args);
          });
          this.prop2 = b;
        }
      
        _createClass(
          A,
          [
            {// get和set方法
              key: "value",
              get:
                function get() {
                  console.log("Getting the current value!");
                  return this.prop1;
                },
              set: function set(newValue) {
                console.log("Setting the current value!");
                this.prop1 = newValue;
              }
            },
            {// 普通函數表達式
              key: "Func",
              value: function Func() {
                console.log("Func");
              }
            }
          ],
          [
            {// 靜態普通函數
              key: "staticFunc",
              value: function staticFunc() {
                console.log("staticFunc", this);
              }
            }
          ]
        );
      
        return A;
      })();
      // 靜態屬性定義到類上
      _defineProperty(A, "prop3", 3);
      // 靜態箭頭函數定義到類上
      _defineProperty(A, "staticArrowFunc", function () {
        console.log("staticArrowFunc", A);
      });
      
      var a = new A(3);

      下圖可描述整個編譯結果:

      需要注意的是:

      • 類原型上的方法、類的靜態方法、類的get和set方法默認是不可枚舉的
      • 類實例上的方法和屬性、類的靜態箭頭函數是可枚舉的

       

      之后再出一章class繼承的相關內容

      博客園  ©  2004-2025
      浙公網安備 33010602011771號 浙ICP備2021040463號-3

      主站蜘蛛池模板: 色爱综合激情五月激情| 国产69成人精品视频免费| 亚洲区中文字幕日韩精品| 色狠狠色噜噜AV一区| 亚洲色婷婷久久精品av蜜桃久久| 女人香蕉久久毛毛片精品| 亚洲综合av男人的天堂| 麻豆成人传媒一区二区| 国产露脸无套对白在线播放| 国产福利视频区一区二区| 国内自拍视频一区二区三区| 四虎永久免费很黄的视频| 国产亚洲一二三区精品| 午夜成人理论无码电影在线播放| 日韩精品福利一二三专区| 最近中文字幕完整版| 密云县| 日产日韩亚洲欧美综合下载| 亚洲一区二区偷拍精品| 国产精品天天看天天狠| 丁香婷婷综合激情五月色| 亚洲欧洲一区二区综合精品| 国产精品免费AⅤ片在线观看 | 男女动态无遮挡动态图| 免费看成人欧美片爱潮app| 激情综合色区网激情五月| 亚洲国产精品综合久久20| 久久久亚洲精品无码| 亚洲国语自产一区第二页| 深夜精品免费在线观看| 亚洲精品一品区二品区三品区| 国产成人无码免费视频麻豆| 激情的视频一区二区三区| 九九久久自然熟的香蕉图片| 欧美裸体xxxx极品| 日韩精品一区二区av在线| 欧美 日韩 国产 成人 在线观看| 人妻中文字幕精品一页| 水蜜桃视频在线观看免费18| 国产日产精品系列| 欧美牲交a欧美牲交aⅴ图片 |