N
nick
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);
// 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
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);
// 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