473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
4
by: Robert Scarborough | last post by:
I have a Table in a Typed Dataset which contains a Date field called EventDate. I've ensured that the field is defined as Date as opposed to DateTime in the Typed Dataset. When I generate an...
8
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
1
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class...
31
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
3
by: davidfinance | last post by:
Ok, maybe this is a stupid question, but why can't I make a subclass of datetime.date and override the __init__ method? --- from datetime import date class A(date): def __init__(self, a,...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
4
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
1
by: Christian Heimes | last post by:
Rick King schrieb: datetime.date is a C extension class. Subclassing of extension classes may not always work as you'd expect it. Christian
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.