Frozen Date objects?
Nathan Wall
nathan.wall at live.com
Fri Jul 19 08:01:25 PDT 2013
Erik Arvidsson wrote:
> This can either be done by exposing an object that has the same
> interface as Date. This could throw on set or it could skip the set
> methods entirely. If you want instanceof to work then all the methods
> needs to be overridden.
Overriding the methods on a real date object (or one that inherits from Date but was constructed with the Date constructor) won't work because then Date.prototype.set* methods would still be able to mutate the internal date by calling them explicitly:
var readOnlyDate = getReadOnlyDate();
readOnlyDate.setHours !== Date.prototype.setHours; // => true
Date.prototype.setHours.call(readOnlyDate, 2); // oops
Also, I don't think `insanceof` should be used to check Dates. If Object.prototype.toString is used, the object just needs to be tagged as a "Date" and the function used to determine whether an object is a date needs to take into consideration "~Date" as well.
Nathan
More information about the es-discuss
mailing list