Date subclass

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I would like to subclass the built-in Date object to get additional
functionality out of it, but the code below gives me an error
something like "object is not a date object". Is there something I'mm
doing wrong?

function foo() {
}
foo.prototype=new Date();
alert( new foo().getHours() );
 
M

Martin Honnen

Christopher said:
I would like to subclass the built-in Date object to get additional
functionality out of it, but the code below gives me an error
something like "object is not a date object". Is there something I'mm
doing wrong?

function foo() {
}
foo.prototype=new Date();
alert( new foo().getHours() );

The ECMAScript specification says clearly:

15.9.5 Properties of the Date Prototype Object
The Date prototype object is itself a Date object (its [[Class]] is
"Date") whose value is NaN.
The value of the internal [[Prototype]] property of the Date prototype
object is the Object prototype
object (15.2.3.1).
In following descriptions of functions that are properties of the Date
prototype object, the phrase “this
Date object†refers to the object that is the this value for the
invocation of the function. None of these
functions are generic; a TypeError exception is thrown if the this value
is not an object for which the
value of the internal [[Class]] property is "Date".

so an implementation is supposed to throw an error if you try to call a
method like getHours on an object that is not a Date object.

Perhaps it suffices for you if you extend Date e.g.

Date.prototype.yourMethod = function (...) { ... };
 
C

Christopher Benson-Manica

Martin Honnen said:
so an implementation is supposed to throw an error if you try to call a
method like getHours on an object that is not a Date object.

I suppose that explains why Google didn't help - thank you.
Perhaps it suffices for you if you extend Date e.g.
Date.prototype.yourMethod = function (...) { ... };

Yes, it probably will do...
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 4 Feb 2005
17:26:18, seen in Christopher Benson-Manica
I would like to subclass the built-in Date object to get additional
functionality out of it,

Read the newsgroup FAQ, and thereby find js-date8.htm "Enhancing the
Object." In that, Day-of-Year and Julian Date methods are added. It
would probably be more useful to add ISO 8601 week number methods (week
number code is in js-date7.htm), and some I/O (done in include3.js).
but the code below gives me an error
something like "object is not a date object". Is there something I'mm
doing wrong?

That seems a reasonable deduction.
function foo() {
}
foo.prototype=new Date();
alert( new foo().getHours() );

Basic ISO 8601 week number methods now added to js-date8.htm. Test!


Note : to get Y M D h m s out of a Date Object, as numbers, one uses six
methods; much of the work is repeated six times. It might be possible
to add cacheing versions of output methods, which retain their last
output and the valueOf used for it. When called, if valueOf matches
cache then return previous result, else call standard method and cache
the result.
 
C

Christopher Benson-Manica

Dr John Stockton said:
Read the newsgroup FAQ,

Ugh, sorry for not having done so. My apologies!
Note : to get Y M D h m s out of a Date Object, as numbers, one uses six
methods; much of the work is repeated six times. It might be possible
to add cacheing versions of output methods, which retain their last
output and the valueOf used for it. When called, if valueOf matches
cache then return previous result, else call standard method and cache
the result.

I may try that sometime when I have free time...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top