Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Babel扩展Date类_Javascript_Date_Inheritance_Ecmascript 6 - Fatal编程技术网

Javascript 使用Babel扩展Date类

Javascript 使用Babel扩展Date类,javascript,date,inheritance,ecmascript-6,Javascript,Date,Inheritance,Ecmascript 6,我想使用extends从Date类扩展一个类 "use strict"; class XDate extends Date { format () { return "foo"; } } let d = new XDate(); console.log(d.format()); console.log(d.getFullYear()); 在本机运行此ES2015工具时,它确实可以正常工作。但当将其传输到ES5时,它不再工作: "use strict"; v

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

"use strict";

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

let d = new XDate();
console.log(d.format());
console.log(d.getFullYear());
在本机运行此ES2015工具时,它确实可以正常工作。但当将其传输到ES5时,它不再工作:

"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());
那么,我如何才能在不破坏代码的情况下实现这一点呢?

由于ES5的限制,Babel无法开箱即用

内置子类应根据具体情况进行评估 as类,如
HTMLElement
可以子类化,而许多类,如
日期
数组
错误
不能是由于ES5发动机的限制

你可以试试这个插件。它没有明确地将
Date
列为受支持的用例之一。

由于ES5的限制,Babel无法开箱即用

内置子类应根据具体情况进行评估 as类,如
HTMLElement
可以子类化,而许多类,如
日期
数组
错误
不能是由于ES5发动机的限制

你可以试试这个插件。它没有明确地将
Date
列为受支持的用例之一

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

TypeError: this is not a Date object.