Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #9202

Re: toString Method with Parameters

From Dr J R Stockton <reply1150@merlyn.demon.co.uk>
Newsgroups comp.lang.javascript
Subject Re: toString Method with Parameters
Date 2011-12-13 20:42 +0000
Organization Home
Message-ID <9qpAjQP5i75OFw+H@invalid.uk.co.demon.merlyn.invalid> (permalink)
References <4g1de7t607d0sdudjmdv4mr1o4cm5hvekg@4ax.com>

Show all headers | View raw


In comp.lang.javascript message <4g1de7t607d0sdudjmdv4mr1o4cm5hvekg@4ax.
com>, Mon, 12 Dec 2011 15:04:33, Gene Wirchenko <genew@ocis.net> posted:

>     Is it legal to define a toString() for an object with the
>toString() having one or more parameters?
>
>     I am going to be defining a date type that does more than the
>usual implementation does.


You have much to learn.

Have you considered <http://www.merlyn.demon.co.uk/js-dobj2.htm> ?
Etc.?

>  (I use certain functionalities fairly
>heavily in my existing app and would like to have them in the Web
>version.)  One of the things that I need is multiple formats.  I could
>have:
>          toString()              like Date's toString()
>          ISO8601DateFormat()     date in ISO 8601 format

But which one - basic or extended (expanded or not), common or ordinal
or week-numbering?  With time?  With offset indicator?

>          IMADateFormat()         date in IMA (internal) format

Never heard of it.  Describe it, and I'll be able to see whether DOBJ2
currently handles it. ... If I read your code correctly, and if your
code is correct, it's just ISO 8601 common basic format.

If you know that 1000 <= year <=9999,
that can be coded more briefly as String(Y*1e4 + M*1e2 + D);
for also 0000-0999,   String(1E8 + Y*1e4 + M*1e2 + D).substring(1).

An 8-digit result converts to extended format with, IIRC,
.replace(/(....)(..)(..)/, "$1-$2-$3")


>or I could have:
>          toString()              (no parm)

The output from that is not defined.  It would be better not to use it
in final code.

>          toString(1) or toString(ISO8601)
>          toString(2) or toString(IMADate)
>
>     I tried it in IE9, and it gives me the results that I want, but
>all that says it that it can work, not that it should.  Is it, in
>fact, legal to overload toString() like this?

It is replacement, not overloading; but a reference to the original can
be saved first.  In DOBJ2, .toString takes a format string argument,
default IIRC provided.

With selector 1, your code does NOT return ISO 8601 for 9 months of the
year and 9 days of each of the rest.  Never claim compliance with an
unfamiliar standard lest you are thought to live south of the nearby
border.


NOTE that, if your code is essentially calendar work, not needing to
read the actual date/time much, it is faster to use the UTC methods; and
then you cannot be troubled with what Those Below quaintly call DST
changes.

I suspect, but have not verified, that you are mixing month numbers
1-12 and 0-11.

It might be wiser to require selector=0 for the Date Object date, and
make no arguments as a fatal error.  That might prevent mistakes.

-- 
 (c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

toString Method with Parameters Gene Wirchenko <genew@ocis.net> - 2011-12-12 15:04 -0800
  Re: toString Method with Parameters Andreas Bergmaier <andber93@web.de> - 2011-12-13 01:14 +0100
    Re: toString Method with Parameters Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-13 02:41 +0100
      Re: toString Method with Parameters Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-13 03:42 +0100
      Re: toString Method with Parameters Dr J R Stockton <reply1150@merlyn.demon.co.uk> - 2011-12-14 20:59 +0000
  Re: toString Method with Parameters Richard Cornford <Richard@litotes.demon.co.uk> - 2011-12-13 04:29 -0800
  Re: toString Method with Parameters Scott Sauyet <scott.sauyet@gmail.com> - 2011-12-13 10:00 -0800
  Re: toString Method with Parameters Dr J R Stockton <reply1150@merlyn.demon.co.uk> - 2011-12-13 20:42 +0000
    Re: toString Method with Parameters Gene Wirchenko <genew@ocis.net> - 2011-12-13 17:48 -0800
      Re: toString Method with Parameters Dr J R Stockton <reply1150@merlyn.demon.co.uk> - 2011-12-15 20:36 +0000
        Re: toString Method with Parameters Gene Wirchenko <genew@ocis.net> - 2011-12-15 19:45 -0800
          Re: toString Method with Parameters Dr J R Stockton <reply1150@merlyn.demon.co.uk> - 2011-12-17 20:59 +0000
            Re: toString Method with Parameters Gene Wirchenko <genew@ocis.net> - 2011-12-19 12:07 -0800
              Re: toString Method with Parameters "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-12-19 20:27 +0000

csiph-web