Please note: this question relates to using the Google Closure compiler. This does not apply directly to ES6, as this part works.
I wrote a class in ES6 that extends the native Date object. This is a great class, but it is a simplified version:
class Dative extends Date {
constructor (dateData) {
super();
super.setTime(Date.parse(dateData));
}
addMilliseconds (ms) {
super.setTime(super.getTime() + ms);
}
}
The above code works fine in Chrome and Firefox. However, when I pass it through the Closure Compiler, it throws errors:
Uncaught TypeError: Method Date.prototype.setTime called on
incompatible receiver [object Object]
Update: calling Date's own methods also fails in the compiled version, but works fine without compiling with a message that it is not a Date object.
I do not understand why code that works in its original form breaks when it compiles.
- , ?
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)
};