为什么 Google Closure Compiler 在扩展 Date() 的 ES6 对象上失败?
Why is Google Closure Compiler failing on my ES6 object which extends Date()?
请注意:这个问题与Google的闭包编译器的使用有关。它不是直接关于 ES6,因为那部分是有效的。
我在 ES6 中写了一个 class,它扩展了原生 Date 对象。这是一个大class,但这是一个简化版本:
class Dative extends Date {
constructor (dateData) {
super();
super.setTime(Date.parse(dateData));
}
addMilliseconds (ms) {
super.setTime(super.getTime() + ms);
}
}
上面的代码在 Chrome 和 Firefox 中运行良好。但是,当我通过 Closure Compiler 传递它时,它会抛出错误:
Uncaught TypeError: Method Date.prototype.setTime called on
incompatible receiver [object Object]
更新:调用本机 Date 方法在编译版本中也失败,但未编译时工作正常,并显示一条消息说这不是 Date 对象。
我不明白的是,为什么以原始形式运行的代码在编译后会中断。
我做错了什么,还是编译器错误?
我使用的是最新版本的 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 环境中运行。来自 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.
不调用 super
它将无法工作。 Date.call(this);
不成功。基本上,如果你将代码转换为 ES5,子类化内置类型是不行的。
所以不,这不是Google关闭的问题。
请注意:这个问题与Google的闭包编译器的使用有关。它不是直接关于 ES6,因为那部分是有效的。
我在 ES6 中写了一个 class,它扩展了原生 Date 对象。这是一个大class,但这是一个简化版本:
class Dative extends Date {
constructor (dateData) {
super();
super.setTime(Date.parse(dateData));
}
addMilliseconds (ms) {
super.setTime(super.getTime() + ms);
}
}
上面的代码在 Chrome 和 Firefox 中运行良好。但是,当我通过 Closure Compiler 传递它时,它会抛出错误:
Uncaught TypeError: Method Date.prototype.setTime called on incompatible receiver [object Object]
更新:调用本机 Date 方法在编译版本中也失败,但未编译时工作正常,并显示一条消息说这不是 Date 对象。
我不明白的是,为什么以原始形式运行的代码在编译后会中断。
我做错了什么,还是编译器错误?
我使用的是最新版本的 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 环境中运行。来自 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.
不调用 super
它将无法工作。 Date.call(this);
不成功。基本上,如果你将代码转换为 ES5,子类化内置类型是不行的。
所以不,这不是Google关闭的问题。