Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

javascript - Extend the Date class using Babel

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());

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());

It ends with this error:

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

TypeError: this is not a Date object.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...