By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,034 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,034 IT Pros & Developers. It's quick & easy.

Date subclass

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Christopher Benson-Manica wrote:
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 (...) { ... };

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

P: n/a
Martin Honnen <ma*******@yahoo.de> spoke thus:
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...

--
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.
Jul 23 '05 #3

P: n/a
JRS: In article <cu**********@chessie.cirr.com>, dated Fri, 4 Feb 2005
17:26:18, seen in news:comp.lang.javascript, Christopher Benson-Manica
<at***@nospam.cyberspace.org> posted :
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.

--
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.
Jul 23 '05 #4

P: n/a
Dr John Stockton <sp**@merlyn.demon.co.uk> spoke thus:
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...

--
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.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.