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


Groups > comp.lang.javascript > #18993

Re: Accessing Constructor Arguments

Message-ID <1610802.2Mv3U4WyR5@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-03-26 18:32 +0100
Subject Re: Accessing Constructor Arguments
Newsgroups comp.lang.javascript
References <a75c7d49-b129-4d92-a7a1-2bce31b71ce4@googlegroups.com>

Show all headers | View raw


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.

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


Thread

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

csiph-web