ES6类继承可以转换为等效的ES5代码吗?

问题描述 投票:0回答:2

[This answer显示了一个简单的ES6类:

class A {
  constructor() {
    this.foo = 42;
  }

  bar() {
    console.log(this.foo);
  }
}

等效于以下ES5代码:

function A() {
  this.foo = 42;
}

A.prototype.bar = function() {
  console.log(this.foo);
}

是否有可能将ES6类继承转换为ES5代码?与下面的派生类等效的ES5是什么?

class B extends A {
  constructor() {
    super();
    this.foo2 = 12;
  }

  bar() {
    console.log(this.foo + this.foo2);
  }

  baz() {
    console.log(this.foo - this.foo2);
  }
}
javascript class inheritance ecmascript-6 prototype
2个回答
0
投票

class A {
  constructor() {
    this.foo = 42;
  }

  bar() {
    console.log(this.foo);
  }
}

class B extends A {
  constructor() {
    super();
    this.foo2 = 12;
  }

  bar() {
    console.log(this.foo + this.foo2);
  }

  baz() {
    console.log(this.foo - this.foo2);
  }
}

可以转换为ES5,>]

"use strict";

function _typeof(obj) { 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }

function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, 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 A =
/*#__PURE__*/
function () {
  function A() {
    _classCallCheck(this, A);

    this.foo = 42;
  }

  _createClass(A, [{
    key: "bar",
    value: function bar() {
      console.log(this.foo);
    }
  }]);

  return A;
}();

var B =
/*#__PURE__*/
function (_A) {
  _inherits(B, _A);

  function B() {
    var _this;

    _classCallCheck(this, B);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(B).call(this));
    _this.foo2 = 12;
    return _this;
  }

  _createClass(B, [{
    key: "bar",
    value: function bar() {
      console.log(this.foo + this.foo2);
    }
  }, {
    key: "baz",
    value: function baz() {
      console.log(this.foo - this.foo2);
    }
  }]);

  return B;
}(A);

使用https://babeljs.io/en/repl查看从ES6到ES5的转换。


0
投票

从之前的工作方式上(等效的行为(忽略属性可枚举之类,并从与ES5兼容的代码扩展实际的ES6类)到:]]

  • 将子原型设置为继承父原型的新对象
© www.soinside.com 2019 - 2024. All rights reserved.