首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >babeljs不能正确地转换扩展类

babeljs不能正确地转换扩展类
EN

Stack Overflow用户
提问于 2016-01-25 22:11:45
回答 1查看 616关注 0票数 3

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

代码语言:javascript
复制
"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浏览器和node5上工作,但是当我使用babeljs传输到ES5 (ES2015预设)时,它就不起作用了:

代码语言:javascript
复制
"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());

为什么?

我在巴贝尔博士上找到了答案

部分支撑 内置子类的可分类性应该根据具体情况进行评估,因为类(如HTMLElement )可以被子类化,而许多类(如日期、数组和错误)不能由于ES5引擎的限制而产生。

如果babel在看到本地对象扩展(它不支持)时抛出一个错误,而不是假装一切都是美好的,并产生错误的代码,我会很感激。

EN

回答 1

Stack Overflow用户

发布于 2016-01-25 22:15:48

根据ECMAScript 6兼容性表的说法,BabelJS不支持类扩展。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35003413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档