使用Babel扩展Date类 [英] Extend the Date class using Babel

查看:34
本文介绍了使用Babel扩展Date类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 extends Date 类扩展一个类.

I want to extend a class from the Date class, using extends.

"use strict";

class XDate extends Date {
    format () {
        return "foo";
    }
}

let d = new XDate();
console.log(d.format());
console.log(d.getFullYear());

在本地运行此ES2015东西时,它确实可以正常工作.但是当将其转换为ES5时,它不再起作用:

It does work fine when running this ES2015 stuff natively. But when transpiling it into ES5, it's not working anymore:

"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 XDate = function (_Date) {
    _inherits(XDate, _Date);

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

        return _possibleConstructorReturn(this, Object.getPrototypeOf(XDate).apply(this, arguments));
    }

    _createClass(XDate, [{
        key: "format",
        value: function format() {
            return "foo";
        }
    }]);

    return XDate;
}(Date);

var d = new XDate();
console.log(d.format());
console.log(d.getFullYear());

它以以下错误结尾:

console.log(d.getFullYear());
              ^

TypeError: this is not a Date object.

那么,如何在不破坏代码的情况下对其进行美化呢?

So, how can I babelify this code without breaking it?

推荐答案

Babel无法.

Babel cannot subclass a lot of built-ins out of the box due to ES5 limitations.

内置子分类性应根据具体情况进行评估因为诸如 HTMLElement 之类的子类可以被子类化,而诸如 Date Array Error 不能是由于ES5引擎的限制.

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.

您可以尝试使用 transform-b​​uiltin-extend 插件.它没有明确列出 Date 作为受支持的用例之一.

You might try the transform-builtin-extend plugin. It doesn't explicitly list Date as one of the supported use cases.

这篇关于使用Babel扩展Date类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆