为什么我的ES6对象延伸Date()时,Google Closure Compiler失败? [英] Why is Google Closure Compiler failing on my ES6 object which extends Date()?

查看:565
本文介绍了为什么我的ES6对象延伸Date()时,Google Closure Compiler失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:此问题涉及使用Google的Closure编译器。这不是直接关于ES6,因为该部分正在运行。



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

  class Dative extends Date {

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


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

}

上面的代码在Chrome中正常工作和Firefox。但是,当我通过Closure编译器时,它会抛出错误:


 未捕获TypeError:方法日期.prototype.setTime调用
不兼容的接收者[object Object]


更新:调用native日期方法在编译版本中也失败,但工作正常,未编译,并显示一条消息,表示这不是Date对象。



我不明白的是为什么在编译时以原始形式运行的代码会中断。



我做错了,或者是编译器bug?



我使用的是最新版本的compiler.jar。作为参考,这是封闭编译器生成的:

  var $ jscomp = {
scope:{},
继承: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


解决方案

日期在ES5中不可分类。所以你想要的是不可能在ES5环境中首先。



泛型代码在ES6环境中也无法工作。从规格


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


不调用 super 它将无法正常工作。 Date.call(this); 不会诀窍。基本上,如果你把代码转换成ES5,那么对内置类型进行子类化就是一个不用了。



所以不,这不是Google Closure的问题。

Please note: this question relates to the use of Google's Closure Compiler. It's not directly about ES6, since that part is working.

I've written a class in ES6 which extends the native Date object. It's a large class, but this is a simplified version:

   class Dative extends Date {

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


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

   }

The code above works fine in Chrome and Firefox. However, when I pass it through Closure Compiler, it throws errors:

Uncaught TypeError: Method Date.prototype.setTime called on
incompatible receiver [object Object]

Update: calling native Date methods also fails in the compiled version but works fine uncompiled, with a message saying this is not a Date object.

What I don't understand is why code that works in its original form breaks when it's been compiled.

Am I doing something wrong, or is this a compiler bug?

I'm using the latest version of compiler.jar. For reference, this is what closure compiler produces:

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 is not subclassable in ES5. So what you want is not possible in ES5 environments in the first place.

The transpiled code cannot work in ES6 environments either. From the specification

The Date constructor is designed to be subclassable. It may be used as the value of an extends clause of a class definition. Subclass constructors that intend to inherit the specified Date behaviour must include a super call to the Date constructor to create and initialize the subclass instance with a [[DateValue]] internal slot.

Without calling super it won't work. Date.call(this); doesn't do the trick. Basically, if you transpile code to ES5, subclassing built-in types is a no-go.

So no, it's not a problem of Google Closure.

这篇关于为什么我的ES6对象延伸Date()时,Google Closure Compiler失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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