From: nick on
I decided to I wanted to subclass the native Date object, but realized
that you can't call Date.prototype's function properties with anything
but a 'pure' date object as the execution context. So, I decided to
wrap all of Date.prototype's important functions, which worked out
pretty well...

The weirdness comes into play with the toString and valueOf methods.
In particular, my MyDate class gives different values for (''+new
MyDate) and (new MyDate.toString()), which confuses me. I didn't
expect the results to differ from (''+new Date) and (new
Date.toString()).

Here's some example code to reproduce it...

/** datetest.js */

// MyDate constructor
var MyDate = function () {
this._date = new Date();
}

// MyDate class definition
MyDate.prototype = (function(){

// for instanceof
var Clone = new Function;
Clone.prototype = Date.prototype;
var base = new Clone;

// native methods of Date.prototype
var _nm = ['getDate','getDay','getFullYear','getHours',
'getMilliseconds','getMinutes','getMonth','getSeconds',
'getTime','getTimezoneOffset','getUTCDate','getUTCDay',
'getUTCFullYear','getUTCHours','getUTCMilliseconds',
'getUTCMinutes','getUTCMonth','getUTCSeconds','getYear',
'setDate','setFullYear','setHours','setMilliseconds',
'setMinutes','setMonth','setSeconds','setTime',
'setUTCDate','setUTCFullYear','setUTCHours',
'setUTCMilliseconds','setUTCMinutes','setUTCMonth',
'setUTCSeconds','setYear','toDateString','toGMTString',
'toISOString','toJSON','toLocaleDateString',
'toLocaleString','toLocaleTimeString','toString',
'toTimeString','toUTCString','valueOf'];

// create wrappers for native methods
for (var i=_nm.length; i--;) (function (f) {
base[f] = function() {
return Date.prototype[f].apply(this._date, arguments);
}
})(_nm[i]);

// our class prototype
return base;

})();

// testing it out

var c = console;

c.log("======== Date ========");
c.log("new Date() : ", new Date());
c.log("''+new Date() : ", ''+new Date());
c.log("(new Date()).toString() : ", (new Date()).toString());
c.log("(new Date()).valueOf() : ", (new Date()).valueOf());

c.log("======== MyDate ========");
c.log("new MyDate() : ", new MyDate());
c.log("''+new MyDate() : ", ''+new MyDate());
c.log("(new MyDate()).toString() : ", (new MyDate()).toString());
c.log("(new MyDate()).valueOf() : ", (new MyDate()).valueOf());

/// EOF

Anyone have any idea what's going on here?

-- Nick
From: Asen Bozhilov on
nick wrote:

> I decided to I wanted to subclass the native Date object, but realized
> that you can't call Date.prototype's function properties with anything
> but a 'pure' date object as the execution context. So, I decided to
> wrap all of Date.prototype's important functions, which worked out
> pretty well...

That design is not really good decision. It is memory inefficient and
break forward compatible with non generics methods for Date
instances.

> The weirdness comes into play with the toString and valueOf methods.
> In particular, my MyDate class gives different values for (''+new
> MyDate) and (new MyDate.toString()), which confuses me. I didn't
> expect the results to differ from  (''+new Date) and (new
> Date.toString()).

By specification that is normal behavior, because Date instances have
special [[DefaultValue]]. During evaluation of addition expression,
objects will be converted to primitive value. For type converting will
be use internal [[DefaultValue]] of that object, without passing hint
argument.

| ECMA-262-3
| 8.6.2.6 [[DefaultValue]] (hint)
| When the [[DefaultValue]] method of O is called with no hint,
| then it behaves as if the hint were Number, unless O is
| a Date object (section 15.9), in which case
| it behaves as if the hint were String.

From that quotation when you create instance object:

var obj = new MyDate();

For object referred by `obj' if call [[DefaultValue]] method with no
hint, will be treat as hint is Number.




From: Johannes Baagoe on
nick :

> True, but can you think of a more efficient way to to extend Date? Or do
> you think I should scrap the idea of extending Date altogether and
> instead make other classes / functions that act on instances of Date?

When I had a similar project some time ago, what I finally decided (with
some help from this newsgroup) was not to subclass, but to combine. That
is, I made a conctructor that returned not an augmented Date object, but
an Object containing a Date object used internally by the various methods.

The result is here: http://baagoe.com/en/ES/XsDateTime.js

Feel free to criticise, copy and use.

--
Johannes
From: nick on
On Apr 20, 12:19 am, Johannes Baagoe <baa...(a)baagoe.com> wrote:
[...]
> When I had a similar project some time ago, what I finally decided (with
> some help from this newsgroup) was not to subclass, but to combine. That
> is, I made a conctructor that  returned not an augmented Date object, but
> an Object containing a Date object used internally by the various methods..
>
> The result is here:http://baagoe.com/en/ES/XsDateTime.js

We're doing a very similar thing, wrapping all the generic
functions :)

You wrote:

for (var i = 0; i < methods.length; i++) {
(function(method){
XsDateTime.prototype[method] = function() {
return Date.prototype[method].apply(this.date, arguments);
};
})(methods[i]);
}

I wrote:

for (var i=_nm.length; i--;) (function (f) {
base[f] = function() {
return Date.prototype[f].apply(this._date, arguments);
}
})(_nm[i]);


> Feel free to criticise, copy and use.

You stole my idea! Just kidding. But hey, I worked out the weirdness
with toString vs. valueOf... going to post about it in response to my
first post so it doesn't get lost.

-- Nick
From: nick on
I think I worked out. In the class prototype definition:

base.valueOf = function(){
return new Number(this._date.valueOf())
}

Looks like because valueOf returns an object and not a primitive,
toString gets used instead with the plus operator (just like Date
instances). MyDates seems to act just like Dates now (but they're
extendable).

-- Nick