javascript - babeljs 不能正确地转换扩展类

标签 javascript ecmascript-6 babeljs

<分区>

我从 MDN 得到了这个示例代码(稍微修改以打印结果):

"use strict";

class myDate extends Date {
  constructor() {
    super();
  }

  getFormattedDate() {
    var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    return this.getDate() + "-" + months[this.getMonth()] + "-" + this.getFullYear();
  }
}

var testDate = new myDate();
document.write(testDate.getFormattedDate());

它适用于 Google Chrome 浏览器和 node5,但是当我使用 babeljs 将它转译为 ES5(ES2015 预设)时它不起作用:

"use strict";

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var myDate = function (_Date) {
  _inherits(myDate, _Date);

  function myDate() {
    _classCallCheck(this, myDate);

    return _possibleConstructorReturn(this, Object.getPrototypeOf(myDate).call(this));
  }

  _createClass(myDate, [{
    key: 'getFormattedDate',
    value: function getFormattedDate() {
      var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
      return this.getDate() + "-" + months[this.getMonth()] + "-" + this.getFullYear();
    }
  }]);

  return myDate;
}(Date);

var testDate = new myDate();
document.write(testDate.getFormattedDate());

为什么?

我在 babel docs 中找到了答案:

Partial support
Built-in subclassability should be evaluated on a case-by-case basis as classes such as HTMLElement can be subclassed while many such as Date, Array and Error cannot be due to ES5 engine limitations.

如果 babel 在看到 native 对象扩展(它不支持)时抛出错误,而不是假装一切顺利并产生错误代码,我将不胜感激:/

最佳答案

根据 ECMAScript 6 compatibility table , BabelJS 不支持类扩展。

关于javascript - babeljs 不能正确地转换扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35003413/

相关文章:

reactjs - 如何从 React 中的 eslint 解析器中排除 css 文件

javascript - Babel 'protoToAssign' 变压器和 ES6 类继承

Javascript - 检查亚马逊对象以查看是否已定义

javascript - 当对集合使用 getter 时,ASP.NET MVC3 模型 Binder 数据无效

javascript - 模式弹出窗口未显示在另一个模式弹出窗口之上

javascript - 在 ES6 中创建带箭头或不带箭头的顶级函数的优缺点是什么?

javascript - 基于配置的Webpack多重 bundle

ember.js - babel 将 "this"导出为 ember 计算属性中的未定义

javascript - 在每个循环中为文本添加颜色更改 - JavaScript

javascript - Image.onload 回调触发不一致