Connecting Tech Pros Worldwide Help | Site Map

Date subclass

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 05:35 PM
Christopher Benson-Manica
Guest
 
Posts: n/a
Default Date subclass

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.

  #2  
Old July 23rd, 2005, 05:35 PM
Martin Honnen
Guest
 
Posts: n/a
Default 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/
  #3  
Old July 23rd, 2005, 05:35 PM
Christopher Benson-Manica
Guest
 
Posts: n/a
Default 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.
  #4  
Old July 23rd, 2005, 05:35 PM
Dr John Stockton
Guest
 
Posts: n/a
Default 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.
  #5  
Old July 23rd, 2005, 05:36 PM
Christopher Benson-Manica
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.