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


Groups > comp.lang.javascript > #18992 > unrolled thread

Accessing Constructor Arguments

Started byddjolley@gmail.com
First post2013-03-26 09:59 -0700
Last post2013-03-27 20:56 +0000
Articles 5 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Accessing Constructor Arguments ddjolley@gmail.com - 2013-03-26 09:59 -0700
    Re: Accessing Constructor Arguments Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-26 18:32 +0100
    Re: Accessing Constructor Arguments JJ <jaejunks@nah.meh> - 2013-03-27 05:53 +0700
    Re: Accessing Constructor Arguments "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-26 23:59 +0100
      Re: Accessing Constructor Arguments Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-27 20:56 +0000

#18992 — Accessing Constructor Arguments

Fromddjolley@gmail.com
Date2013-03-26 09:59 -0700
SubjectAccessing Constructor Arguments
Message-ID<a75c7d49-b129-4d92-a7a1-2bce31b71ce4@googlegroups.com>
My objective is to add an is_date() method to the Date class.  This method is to be used in validating dates.

Interestingly, when a new instance of Date is created, if the date supplied via the arguments is not a valid date, the date is automatically adjusted to its nearest valid neighbor.  So, the validity of a date used to create an instance of Date can be easily verified by simply comparing the date that is accessible via the various methods of the instance to the date supplied in the constructor arguments.  My problem is that I don't know how to access the constructor arguments from within the new method that I am creating so that I can make the comparison.

Can someone please help me with this?  I hope that I have been clear in articulating my question.  Thanks for any input.

      ...doug

[toc] | [next] | [standalone]


#18993

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-03-26 18:32 +0100
Message-ID<1610802.2Mv3U4WyR5@PointedEars.de>
In reply to#18992
ddjolley@gmail.com wrote:

> My objective is to add an is_date() method to the Date class.  This method
> is to be used in validating dates.
> 
> Interestingly, when a new instance of Date is created, if the date
> supplied via the arguments is not a valid date, the date is automatically
> adjusted to its nearest valid neighbor.

In a manner of speaking.  What really happens is specified in the ECMAScript 
Language Specification, (currently) 5.1 Edition.

> So, the validity of a date used to create an instance of Date can be
> easily verified by simply comparing the date that is accessible via the
> various methods of the instance to the date supplied in the constructor
> arguments.  My problem is that I don't know how to access the constructor
> arguments from within the new method that I am creating so that I can make
> the comparison.
> 
> Can someone please help me with this?  I hope that I have been clear in
> articulating my question.  Thanks for any input.

Your line-breaks are broken in that they are virtually non-existent.  Please 
do not use Google Groups for posting to Usenet, and post using a real name, 
for that matter.

Your question is considerably unclear.  First of all, there is no “Date 
class”.  You are dealing with languages that use prototype-based inheritance 
here.  There is a Date object that is a constructor (Function instance) that 
can also be called as a function (without “new”).  That object has a 
“prototype” property referring to an object (the “Date prototype” in ES 
terminology) whose properties are inherited by Date instances (objects 
created calling that function as a constructor).  It also has other 
properties that are not implicitly inherited.

It is unclear which object your is_date() should be a method of.  It is 
unclear what its formal parameters should be.

It should be obvious that an is_date() method (ES convention calls for 
isDate() here) without formal parameters will not be able to tell whether 
the values originally passed to the Date constructor constitute a proper 
date because Date instances do not store those values; they store a time 
value (milliseconds since epoch) computed from those values, and, as you 
observed correctly, the arguments to the Date constructor are adjusted 
internally if they do not specify a proper date (like February 29 when not 
in a leap year).

It should also be obvious that by comparing the values passed to a Date 
constructor with the values retrieved from a Date instance thus created 
(using the methods of the Date prototype), you will be able to tell whether 
overflow or underflow occurred on construction only as long as you are 
(somewhat) in charge of object construction.

I could hand you the solution (it is fairly trivial), and there may be 
people here who will, but it is better for you to figure it out by yourself 
(you may ask if there are actual implementation difficulties).

Instead, I would like to point out that JSX:object.js augments the Function 
prototype object with a construct() method [1] that can be helpful here.  
JSX is Free Software distributed under the GNU GPLv3.

HTH

[1] 
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fobject.js>
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#19009

FromJJ <jaejunks@nah.meh>
Date2013-03-27 05:53 +0700
Message-ID<1f7xegkzq9acn$.113l8gtblt89m$.dlg@40tude.net>
In reply to#18992
On Tue, 26 Mar 2013 09:59:36 -0700 (PDT), ddjolley@gmail.com wrote:
> My objective is to add an is_date() method to the Date class.  This method
> is to be used in validating dates. 
> 
> Interestingly, when a new instance of Date is created, if the date
> supplied via the arguments is not a valid date, the date is automatically
> adjusted to its nearest valid neighbor.  So, the validity of a date used
> to create an instance of Date can be easily verified by simply comparing
> the date that is accessible via the various methods of the instance to
> the date supplied in the constructor arguments.  My problem is that I
> don't know how to access the constructor arguments from within the new
> method that I am creating so that I can make the comparison. 
> 
> Can someone please help me with this?  I hope that I have been clear in
> articulating my question.  Thanks for any input. 
> 
> ...doug

The Date object's constructor is a native function, so you won't be able to
modify the code in that function. You might want to create a wrapper/helper
class instead, where you can check/alter the argument before and/or after
passing it to the Date constructor. For example...

function MyDate(arg) {
  /*
  put arg checking/adjustment code here
  */
  var result = new Date(arg);
  /*
  put additional date checking/correction code here
  */
  return result; //result is still a Date object, not MyDate object
}
MyDate.is_date = function(date) {
  return !isNaN(date.valueOf());
};
var date = new MyDate("12/31/2000");
var valid = MyDate.is_date(date);

You can add the is_date method into the Date prototype so that the method
will be accessible from all Date objects, but it's not a recommended method.

[toc] | [prev] | [next] | [standalone]


#19010

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-03-26 23:59 +0100
Message-ID<XnsA18FF4162252Eeejj99@194.109.133.133>
In reply to#18992
 wrote on 26 mrt 2013 in comp.lang.javascript:

> Interestingly, when a new instance of Date is created, if the date
> supplied via the arguments is not a valid date, the date is
> automatically adjusted to its nearest valid neighbor. 

You ony have to check the validity of two[!] 
of the three parts of the resulting date, 
something like:

var tempA = myDateString.split(/[-.\/]/); // d-m-yyyy expected

// [test for the array size being 3, and the components being numeric]

var tempD = new Date(tempA[2],tempA[1]-1,tempA[0]);

if (tempA[1]-1 != tempD.getMonth()) ... error

if (tempA[2] != tempD.getFullYear()) ... error

// now tempD.getDate() must be correct by logic.


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#19035

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-03-27 20:56 +0000
Message-ID<Z2sAGlLT01URFwP1@invalid.uk.co.demon.merlyn.invalid>
In reply to#19010
In comp.lang.javascript message <XnsA18FF4162252Eeejj99@194.109.133.133>
, Tue, 26 Mar 2013 23:59:40, Evertjan. <exxjxw.hannivoort@inter.nl.net>
posted:

> wrote on 26 mrt 2013 in comp.lang.javascript:
>
>> Interestingly, when a new instance of Date is created, if the date
>> supplied via the arguments is not a valid date, the date is
>> automatically adjusted to its nearest valid neighbor.
>
>You ony have to check the validity of two[!]
>of the three parts of the resulting date,
>something like:
>
>var tempA = myDateString.split(/[-.\/]/); // d-m-yyyy expected
>
>// [test for the array size being 3, and the components being numeric]
>
>var tempD = new Date(tempA[2],tempA[1]-1,tempA[0]);
>
>if (tempA[1]-1 != tempD.getMonth()) ... error
>
>if (tempA[2] != tempD.getFullYear()) ... error
>
>// now tempD.getDate() must be correct by logic.


For "two[!]" read "one[!!]".

Do the split with a crafty RegExp, thereby ensuring that the day-of-
month lies in '00' to '99; create a new Date Object from the split, and
check that the Month "in" the Object agrees with the month in the split
string.

But, of course, it is not possible to check a date string for validity
without more information - is 1712/02/30 valid? it is, and the location
must be not very far to the NE of the Netherlands.  Is 1752-09-12 valid?
not here, and I think I therefore once remarked to my mother on her
birthday that she could not possibly have been exactly some specific
considerable number of decades older.  Is 14/11/1948 valid? of course,
it is the DoB of a.k.a. the Earl of Chester.  Are 2013-W13-3 and 2013086
valid?  Yes, and they represent today (Wednesday), and so is 56378.

Remember the delightful tale of the American who tried in September YYYY
to enter Europe using a visa marked as expiring 11.06.YYYY (a genuine
example represented by made-up details).

To check for meaningful validity, one needs in general to know the
Calendar (Gregorian, Julian, Civil (where?), etc.), the notation or
allowable notations (e.g. ISO, Euro-trad, FFF) and if not numeric the
allowable languages.

Remember too that 16-bit Unicode, as used in JavaScript, has at least
two ways of specifying (for the learned such as yourself, and for anyone
educated long enough ago, and for film fans) each of the dozen months of
the Christian calendar with a single character.

The OP should have read the FAQ, which might then have guided him to my
Web site.

The OP's second sentence is incorrect.

-- 
 (c) John Stockton, nr London, UK.   E-mail, see Home Page.    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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web