Connecting Tech Pros Worldwide Forums | Help | Site Map

Date subclass

Christopher Benson-Manica
Guest
 
Posts: n/a
#1: Jul 23 '05
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() );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Martin Honnen
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Date subclass




Christopher Benson-Manica wrote:
[color=blue]
> 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() );[/color]

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 (...) { ... };

--

Martin Honnen
http://JavaScript.FAQTs.com/
Christopher Benson-Manica
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Date subclass


Martin Honnen <mahotrash@yahoo.de> spoke thus:
[color=blue]
> 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.[/color]

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

Yes, it probably will do...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dr John Stockton
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Date subclass


JRS: In article <cu0b7q$pp3$1@chessie.cirr.com>, dated Fri, 4 Feb 2005
17:26:18, seen in news:comp.lang.javascript, Christopher Benson-Manica
<ataru@nospam.cyberspace.org> posted :[color=blue]
>I would like to subclass the built-in Date object to get additional
>functionality out of it,[/color]

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).
[color=blue]
> but the code below gives me an error
>something like "object is not a date object". Is there something I'mm
>doing wrong?[/color]

That seems a reasonable deduction.
[color=blue]
>function foo() {
>}
>foo.prototype=new Date();
>alert( new foo().getHours() );[/color]

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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Christopher Benson-Manica
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Date subclass


Dr John Stockton <spam@merlyn.demon.co.uk> spoke thus:
[color=blue]
> Read the newsgroup FAQ,[/color]

Ugh, sorry for not having done so. My apologies!
[color=blue]
> 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.[/color]

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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Closed Thread