美文网首页
16.es6中class转es5源码解读代码

16.es6中class转es5源码解读代码

作者: 静昕妈妈芦培培 | 来源:发表于2021-11-19 17:40 被阅读0次

    把es6中class转换为es5,通过babel转换
    https://babeljs.io/

    class Person {}
    

    babel转换如下

    //检验instance是否是Constructor构造函数的实例
    function _classCallCheck(instance, Constructor) {
      // instanceof 运算符  用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上
      if (!(instance instanceof Constructor)) {
        // 如果instance不是Constructor构造函数的实例,就报错:不能把构造函数作为一个普通函数调用
        throw new TypeError("Cannot call a class as a function");
      }
    }
    
    var Person = function Person() {
      //Person为一个构造函数,如果被new调用,函数内this为Person实例;
      //如果作为普通函数调用,则this不是Person的实例
      //此函数用来检查this是否为Person的实例,不是就报错
      _classCallCheck(this, Person);
    };
    
    
    
    class Person {
      constructor(name, age) {
      this.name = name
      this.age = age
      }
      running() {}
    }
    

    babel转换

    "use strict";
    
    //检查instance是否为Constructor构造函数的实例
    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;
        //如果当前属性描述符有value属性,就设置可写;如果是存取属性描述符就不需要了
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }
    
    //给构造函数的原型对象上定义一组原型对象属性,给构造函数本身定义一组静态属性
    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);
      if (staticProps) _defineProperties(Constructor, staticProps);
      return Constructor;
    }
    
    var Person = /*#__PURE__*/ (function () {
      function Person(name, age) {
        //检查Person构造函数是否通过new调用,如果作为普通函数调用则报错
        _classCallCheck(this, Person);
    
        this.name = name;
        this.age = age;
      }
      //在Person.prototype上添加running方法
      _createClass(Person, [
        {
          key: "running",
          value: function running() {},
        },
      ]);
    
      return Person;
    })();
    
    
    class Person {
      constructor(name, age) {
        this.name = name;
        this.age = age;
      }
      running() {
       console.log(this.name + "is running");
      }
    }
    class Student extends Person {
      constructor(name, age, num) {
        super(name, age);
        this.num = num;
       }
       eatting() {
         console.log(this.name + "is eatting");
       }
     }
    

    babel转换

    "use strict";
    
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj &&
            typeof Symbol === "function" &&
            obj.constructor === Symbol &&
            obj !== Symbol.prototype
            ? "symbol"
            : typeof obj;
        };
      }
      return _typeof(obj);
    }
    
    
    //实现子类继承父类
    function _inherits(subClass, superClass) {
      if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function");
      }
      // 子类的prototype 指向一个对象,此对象的__proto__指向父类的prototype
      // 子类的prototype添加一个constructor属性,执行子类构造函数
      subClass.prototype = Object.create(superClass && superClass.prototype, {
        constructor: { value: subClass, writable: true, configurable: true },
      });
      // 子类继承父类的静态属性
      if (superClass) _setPrototypeOf(subClass, superClass);
    }
    
    // 设置一个指定的对象的[[prototype]]原型对象重新指向另一个对象或null
    function _setPrototypeOf(o, p) {
      // Object.setPrototypeOf(targetObje, newPrototype) 设置一个指定的对象的[[prototype]]原型对象重新指向另一个对象或null
      _setPrototypeOf =
        Object.setPrototypeOf ||
        function _setPrototypeOf(o, p) {
          o.__proto__ = p;
          return o;
        };
      return _setPrototypeOf(o, p);
    }
    
    //创建一个父类构造函数
    function _createSuper(Derived) {
      // Derived为子类或派生类,即Student
      var hasNativeReflectConstruct = _isNativeReflectConstruct();
      return function _createSuperInternal() {
        //此处的this为子类构造函数中的this
        //获取子类的父类 Student.__proto__为Person
        var Super = _getPrototypeOf(Derived),
          result;
        if (hasNativeReflectConstruct) {
          var NewTarget = _getPrototypeOf(this).constructor;
          result = Reflect.construct(Super, arguments, NewTarget);
        } else {
          // arguments为传递给当前函数的参数集合,为类数组
          // 执行父类构造函数,并把其this绑定为子类构造函数中的this,把参数传入,接收父类构造函数执行返回的对象
          result = Super.apply(this, arguments);
        }
        //检验result只能为对象或undefined,否则报错,并且_createSuperInternal执行时,其中this被绑定为了子类构造函数中的this
        return _possibleConstructorReturn(this, result);
      };
    }
    
    //检验call为只能为对象或undefined,否则报错,并且检验self不为undefined
    function _possibleConstructorReturn(self, call) {
      if (call && (_typeof(call) === "object" || typeof call === "function")) {
        return call;
      } else if (call !== void 0) {
        // void 0代表undefined
        throw new TypeError(
          "Derived constructors may only return object or undefined"
        );
      }
      return _assertThisInitialized(self);
    }
    
    //检验self不为undefined,否则报错
    function _assertThisInitialized(self) {
      if (self === void 0) {
        throw new ReferenceError(
          "this hasn't been initialised - super() hasn't been called"
        );
      }
      return self;
    }
    
    function _isNativeReflectConstruct() {
      if (typeof Reflect === "undefined" || !Reflect.construct) return false;
      if (Reflect.construct.sham) return false;
      if (typeof Proxy === "function") return true;
      try {
        Boolean.prototype.valueOf.call(
          Reflect.construct(Boolean, [], function () {})
        );
        return true;
      } catch (e) {
        return false;
      }
    }
    
    //获取一个对象的[[prototype]]原型对象
    function _getPrototypeOf(o) {
      _getPrototypeOf = Object.setPrototypeOf
        ? Object.getPrototypeOf
        : function _getPrototypeOf(o) {
            return o.__proto__ || Object.getPrototypeOf(o);
          };
      return _getPrototypeOf(o);
    }
    
    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);
      }
    }
    
    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);
      if (staticProps) _defineProperties(Constructor, staticProps);
      return Constructor;
    }
    
    var Person = /*#__PURE__*/ (function () {
      function Person(name, age) {
        _classCallCheck(this, Person);
    
        this.name = name;
        this.age = age;
      }
    
      _createClass(Person, [
        {
          key: "running",
          value: function running() {
            console.log(this.name + "is running");
          },
        },
      ]);
    
      return Person;
    })();
    
    var Student = /*#__PURE__*/ (function (_Person) {
      //子类继承父类
      _inherits(Student, _Person);
      //通过子类获取父类构造函数
      var _super = _createSuper(Student);
    
      function Student(name, age, num) {
        var _this;
        //检查Student是否是通过new调用,如果作为普通函数调用则报错
        _classCallCheck(this, Student);
        //执行父类构造函数,父类构造函数中的this绑定为子类构造函数中的this,父类构造函数执行返回的对象赋值给this
        _this = _super.call(this, name, age);
        _this.num = num;
        return _this;
      }
    
      _createClass(Student, [
        {
          key: "eatting",
          value: function eatting() {
            console.log(this.name + "is eatting");
          },
        },
      ]);
    
      return Student;
    })(Person);
    
    
    

    相关文章

      网友评论

          本文标题:16.es6中class转es5源码解读代码

          本文链接:https://www.haomeiwen.com/subject/amxetrtx.html