Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.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 为什么Google Closure编译器在扩展Date()的ES6对象上失败?_Javascript_Ecmascript 6_Google Closure Compiler - Fatal编程技术网

Javascript 为什么Google Closure编译器在扩展Date()的ES6对象上失败?

Javascript 为什么Google Closure编译器在扩展Date()的ES6对象上失败?,javascript,ecmascript-6,google-closure-compiler,Javascript,Ecmascript 6,Google Closure Compiler,请注意:这个问题与谷歌闭包编译器的使用有关。这与ES6没有直接关系,因为这一部分正在发挥作用 我在ES6中编写了一个类,它扩展了本机日期对象。这是一个大类,但这是一个简化版本: class Dative extends Date { constructor (dateData) { super(); super.setTime(Date.parse(dateData)); } addMillisec

请注意:这个问题与谷歌闭包编译器的使用有关。这与ES6没有直接关系,因为这一部分正在发挥作用

我在ES6中编写了一个类,它扩展了本机日期对象。这是一个大类,但这是一个简化版本:

   class Dative extends Date {

       constructor (dateData) {
           super();
           super.setTime(Date.parse(dateData));
       }


       addMilliseconds (ms) {
           super.setTime(super.getTime() + ms);
       }

   }
上面的代码在Chrome和Firefox中运行良好。但是,当我通过闭包编译器传递它时,它会抛出错误:

更新:在编译版本中调用本机日期方法也会失败,但在未编译的情况下可以正常工作,并显示一条消息,说明这不是日期对象

我不明白的是,为什么以原始形式工作的代码在编译时会中断

我是做错了什么,还是这是一个编译器错误

我使用的是compiler.jar的最新版本。作为参考,以下是闭包编译器生成的内容:

var $jscomp = {
    scope: {},
    inherits: function(a, b) {
        function d() {}
        d.prototype = b.prototype;
        a.prototype = new d;
        a.prototype.constructor = a;
        for (var c in b)
            if (Object.defineProperties) {
                var e = Object.getOwnPropertyDescriptor(b, c);
                e && Object.defineProperty(a, c, e)
            } else
                a[c] = b[c]
    }
}
  , Dative = function(a) {
    Date.call(this);
    Date.prototype.setTime.call(this, Date.parse(a))
};

$jscomp.inherits(Dative, Date);
Dative.UTC = Date.UTC;
Dative.parse = Date.parse;
Dative.now = Date.now;
Dative.prototype.addMilliseconds = function(a) {
    Date.prototype.setTime.call(this, Date.prototype.getTime.call(this) + a)
};
//# sourceMappingURL=./DativeShort.map

Date
在ES5中不是子类。因此,您想要的在ES5环境中首先是不可能的

传输的代码也不能在ES6环境中工作。从

日期构造函数被设计为可子类化。它可以用作类定义的extends子句的值。打算继承指定日期行为的子类构造函数必须包含对日期构造函数的超级调用,以使用[[DateValue]]内部插槽创建和初始化子类实例

如果不调用
super
它将无法工作<代码>日期。呼叫(此)不起作用。基本上,如果您将代码传输到ES5,子类化内置类型是不可能的


所以不,这不是谷歌关闭的问题。

作为一个bug提出:我相信这是正确的。我证实了巴贝尔对这一点的解释也是无效的。谢谢,有用的信息。我希望Close知道如何在ES5中复制ES6行为:(
var $jscomp = {
    scope: {},
    inherits: function(a, b) {
        function d() {}
        d.prototype = b.prototype;
        a.prototype = new d;
        a.prototype.constructor = a;
        for (var c in b)
            if (Object.defineProperties) {
                var e = Object.getOwnPropertyDescriptor(b, c);
                e && Object.defineProperty(a, c, e)
            } else
                a[c] = b[c]
    }
}
  , Dative = function(a) {
    Date.call(this);
    Date.prototype.setTime.call(this, Date.parse(a))
};

$jscomp.inherits(Dative, Date);
Dative.UTC = Date.UTC;
Dative.parse = Date.parse;
Dative.now = Date.now;
Dative.prototype.addMilliseconds = function(a) {
    Date.prototype.setTime.call(this, Date.prototype.getTime.call(this) + a)
};
//# sourceMappingURL=./DativeShort.map