Inherit Date Class?

C

Chris

Hi,

I'm kind of new to the more involved workings of javascript, and am
having a few problems.

My problem is this: I am working with a CMS which is including various
javascrip files in the pages, including files for a calendar
(Dynarch).

I would like to add some new functionality to the CMS and add some
extra javascript files from a different calendar (Zapatech). The
Zapatech calendar is veeeeeeery closely based on the Dynarch calendar.

Both calendars extend the Date object, and in many places use the same
prototype function names e.g. Date.prototype.getMonthDays

The problem is that calling Date.getMonthDays, for example, calls the
definition from the Dynarch files (these files are included in the
page first), whereas I want it to call the definition in the Zapatech
files.

I have been looking for a way to apply appropriate namespacing, via
inheriting the Date class into a new class e.g. ZDate, but have not
figured out how to successfully achieve this.

For example, my attempts at the new ZDate class will return an error
when you try ZDate.parse("Jul 8, 2008"); stating that ZDate.parse is
not a function

So my question is how do I succesfully seperate my new javascript from
the already present definitions.

(or alternatively, is there a way to remove all the additions made to
the Date class, and start with a fresh Date version for my new
javascript?)

Any help in this is greatly appreciated!

Chris
 
D

dhtml

Peter said:
Hi Chris
You will find tutorials and blog articles on what you need at
http://developer.yahoo.com/yui/theater/

The Grady Booch video is good.

Some good quotes:

"Microsoft Office will one day consume all the bits in the universe."

And this one:
| The wrong architectural setup that I see in some places is where you
| have an architectural group over here (waves hand) not associated with
| any one project and you end up with people who are generally the
| people who are... they're there because they aren't good enough to be
| on a real project, I'll politely say.
|
| And furthermore, they are so disjointed from the real problem, that
| they're building things that nobody will care about anymore.
|

That sounds a lot like the role of the YUI team, the Yahoo Exceptional
Performance Team, and Douglas Crockford.

Points taken:
The major factor that impacts the performance of a project (its
success) is complexity
A bad process increases complexity, a good one reduces it.

But the yahoo video controls have a lot of problems (kept popping up
favorites)

Then when I try and fast forward it takes me to a login page.

Did you call it before it was available? Before the script containing
|ZDate.parse| was loaded?
 
D

dhtml

dhtml said:
Peter McMurray wrote:
And this one:
| The wrong architectural setup that I see in some places is where you
| have an architectural group over here (waves hand) not associated with
| any one project and you end up with people who are generally the
| people who are... they're there because they aren't good enough to be
| on a real project, I'll politely say.
|
| And furthermore, they are so disjointed from the real problem, that
| they're building things that nobody will care about anymore.
|

That sounds a lot like the role of the YUI team, the Yahoo Exceptional
Performance Team, and Douglas Crockford.

(not to discredit any of them, more to what I see as that organizational
role at Yahoo).
 
C

Chris

Hi,

Thanks for the link. Just to be clear, you were referring to the
videos by Douglas Crockford — "The JavaScript Programming Language",
right?

I will try and get through the 2hours of video tonight, but in the
meantime, I would appreciate it if anyone could make a comment on what
I'm trying to achieve - whether it's incredibly easy to do, whether
it's just not possible etc.

Thanks
 
D

Dr J R Stockton

In comp.lang.javascript message <2aad5f4d-8f31-4f8f-b4a6-22f9ab07379c@d1
g2000hsg.googlegroups.com>, Wed, 8 Oct 2008 21:34:18, Chris <christopher
(e-mail address removed)> posted:
My problem is this: I am working with a CMS which is including various
javascrip files in the pages, including files for a calendar
(Dynarch).

I would like to add some new functionality to the CMS and add some
extra javascript files from a different calendar (Zapatech). The
Zapatech calendar is veeeeeeery closely based on the Dynarch calendar.

Both calendars extend the Date object, and in many places use the same
prototype function names e.g. Date.prototype.getMonthDays

Is there anything that might reasonably be added to Date Objects, by
such systems, that one cannot readily add oneself, guided by freely-
available code and other resources?

I suppose I am assuming that you are using only Gregorian or Julian
dates, AD/BC/astro - not AM, AH, AUC, etc., dates?

If you do use imported libraries, make careful full-range checks. If MS
cannot get things right, should one expect that others will? Perhaps
that had better not be answered.
 
T

Thomas 'PointedEars' Lahn

Chris said:
I would like to add some new functionality to the CMS and add some extra
javascript files from a different calendar (Zapatech). The Zapatech
calendar is veeeeeeery closely based on the Dynarch calendar.

Both calendars extend the Date object,

They extend *the* Date *prototype* object, which is a different animal.
*The* Date object is merely the constructor of Date objects, and it is a
Function object.
and in many places use the same prototype function names e.g.
Date.prototype.getMonthDays

The problem is that calling Date.getMonthDays, for example, calls the
definition from the Dynarch files (these files are included in the page
first), whereas I want it to call the definition in the Zapatech files.

I have been looking for a way to apply appropriate namespacing, via
inheriting the Date class into a new class e.g. ZDate, but have not
figured out how to successfully achieve this.

For example, my attempts at the new ZDate class will return an error when
you try ZDate.parse("Jul 8, 2008"); stating that ZDate.parse is not a
function

Your attempts to do that require a crystal ball to be seen; you should
have posted them. However, usually the proper way to inheritance is:

function inheritFrom(o)
{
function Dummy()
{
}

Dummy.prototype = o;
return new Dummy();
}

function ZDate()
{
}

ZDate.prototype = inheritFrom(Date.prototype);
So my question is how do I succesfully seperate my new javascript from
the already present definitions.

The definitions for `Date.prototype.getMonthDays' would have to be loaded in
order. Therefore, you can make a backup of the reference to the callable
object this property refers to in-between.

<script type="text/javascript" src="zapatech.js"></script>

<script type="text/javascript">
// back up Zapatech version
var f = Date.prototype.getMonthDays;
</script>

<script type="text/javascript" src="dynarch.js"></script>

<script type="text/javascript">
// restore Zapatech version
Date.prototype.getMonthDays = f;

window.alert((new Date()).getMonthDays());
</script>

You can also assign the backed up reference value to a property of your own
object (that can inherit from Date.prototype) and call this property instead:

<script type="text/javascript" src="zapatech.js"></script>
<script type="text/javascript" src="zdate.js"></script>
(or alternatively, is there a way to remove all the additions made to the
Date class, and start with a fresh Date version for my new javascript?)

There is no Date class because there are no classes at all. And no, you
cannot reliably delete all user-defined properties from the Date prototype
object because the built-in properties do not have the DontEnum attribute.

It was a different thing if you implemented a property registry, used this
registry when adding properties to the prototype object and when removing
them from it.

<http://www.crockford.com/javascript/javascript.html>


PointedEars
 
C

Chris

Hi,

I tried the implementation described above for inheritence, followed
by:

var d = ZDate.parse("Jul 8, 2005");

but I still get the same error of "ZDate.parse is not a function"

I'm sure I'm not understanding things properly (I am going to go and
watch several hours of video now to try and catch up), but in the
meantime, can I inherit from the Date prototype (or whatever) such
that ZDate.parse is actually a function (without explicitly defining
it)? Is this possible?

Thanks
 
E

Evertjan.

Chris wrote on 10 okt 2008 in comp.lang.javascript:
I tried the implementation described above for inheritence,

Above what?

[please always quote on usenet]
followed
by:

var d = ZDate.parse("Jul 8, 2005");

but I still get the same error of "ZDate.parse is not a function"

You should use Date, not ZDate.

Date.parse(dateVal)
Parses a string containing a date, and returns the number of milliseconds
between that date and midnight, January 1, 1970.

Try:

<script type='text/javascript'>

alert( Date.parse('Jul 8, 2005') )

</script>
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
You should use Date, not ZDate.

Date.parse(dateVal)
Parses a string containing a date, and returns the number of milliseconds
between that date and midnight, January 1, 1970.

Accurate citation of inaccurate material.

(A) Midnight is generally, or at least often, considered as being at the
end of the day. Granted, anyone intelligent enough to become a
successful programmer will realise that; but many others visit here.

(B) Needs GMT, UTC, or ideally UT.

Epoch is 1970-01-01T00:00:00Z. I'd have transposed the "arguments" of
'between', too.
 
E

Evertjan.

Dr J R Stockton wrote on 11 okt 2008 in comp.lang.javascript:
In comp.lang.javascript message


Accurate citation of inaccurate material.

It is from the Jscript 5.6 specs by MS.
(A) Midnight is generally, or at least often, considered as being at
the end of the day. Granted, anyone intelligent enough to become a
successful programmer will realise that; but many others visit here.

I would never specify "midnight" of a given date in the first place
(B) Needs GMT, UTC, or ideally UT.


I would think it gives the miliseconds difference between zero hours
local time and 1970-01-01T00:00:00Z

However as local time overhere is UTC+2 [CET summertime/DST],

The result of:

document.write( Date.parse('Jan 1, 1970')+1*60*60*1000 )

being zero amazes me,

as I would have expected zero to be the result of

document.write( Date.parse('Jan 1, 1970')+2*60*60*1000+'<br>' )
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Dr J R Stockton wrote on 11 okt 2008 in comp.lang.javascript:

It is from the Jscript 5.6 specs by MS.

A notoriously chronologically-unreliable organisation. Additionally,
the string can contain a time and an optional offset.

I would never specify "midnight" of a given date in the first place

The Dutch are wise.

(B) Needs GMT, UTC, or ideally UT.


I would think it gives the miliseconds difference between zero hours
local time and 1970-01-01T00:00:00Z
Yes.


However as local time overhere is UTC+2 [CET summertime/DST],

The result of:

document.write( Date.parse('Jan 1, 1970')+1*60*60*1000 )

being zero amazes me,

It may be Summer in the Netherlands at present, but it was Winter on
1970/01/01.

as I would have expected zero to be the result of

document.write( Date.parse('Jan 1, 1970')+2*60*60*1000+'<br>' )

Ditto.


Note that the parsing of date/time strings is ill-defined in ECMA &
ISO/IEC : accepting "Zondag 12 oktober 2008" is permissible.

Test, with different months, something like
Date.parse("2008/07/15 12:00 ") % 864e5 / 36e5
with/without UTC in the string.

+new Date(Date.parse(S)) == +new Date(S) // IIRC.
 
E

Evertjan.

Dr J R Stockton wrote on 13 okt 2008 in comp.lang.javascript:
It may be Summer in the Netherlands at present, but it was Winter on
1970/01/01.

Thanks, I didn't remember ;-)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top