G
Gene Wirchenko
Dear JavaScripters:
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. (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
IMADateFormat() date in IMA (internal) format
or I could have:
toString() (no parm)
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?
Here is my code:
***** Start of Experimental Code *****
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>try8.html</title>
</head>
<script type="text/javascript">
function NewDate(Year,Month,Day)
{
this.Year=Year;
this.Month=Month;
this.Day=Day;
NewDate.prototype.toString=NewDateToString;
}
function NewDateToString
(
Selector
)
{
if (NewDateToString.arguments.length==0)
{
var theDate=new Date(this.Year,this.Month,this.Day);
return theDate.toString();
}
if (Selector==1)
return this.Year+"-"+this.Month+"-"+this.Day;
if (Selector==2)
{
var RetVal="";
var Piece="0000"+this.Year;
RetVal+=Piece.substring(Piece.length-4);
Piece="00"+this.Month;
RetVal+=Piece.substring(Piece.length-2);
Piece="00"+this.Day;
RetVal+=Piece.substring(Piece.length-2);
return RetVal;
}
return "Invalid NewDate toString() selector.";
}
</script>
<body>
<script type="text/javascript">
var SomeDay=new NewDate(2011,12,25); // Christmas 2011
alert("Date toString(): "+SomeDay.toString());
alert("ISO 8601 Date: "+SomeDay.toString(1));
alert("IMA Date: "+SomeDay.toString(2));
</script>
</body>
</html>
***** End of Experimental Code *****
Sincerely,
Gene Wirchenko
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. (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
IMADateFormat() date in IMA (internal) format
or I could have:
toString() (no parm)
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?
Here is my code:
***** Start of Experimental Code *****
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>try8.html</title>
</head>
<script type="text/javascript">
function NewDate(Year,Month,Day)
{
this.Year=Year;
this.Month=Month;
this.Day=Day;
NewDate.prototype.toString=NewDateToString;
}
function NewDateToString
(
Selector
)
{
if (NewDateToString.arguments.length==0)
{
var theDate=new Date(this.Year,this.Month,this.Day);
return theDate.toString();
}
if (Selector==1)
return this.Year+"-"+this.Month+"-"+this.Day;
if (Selector==2)
{
var RetVal="";
var Piece="0000"+this.Year;
RetVal+=Piece.substring(Piece.length-4);
Piece="00"+this.Month;
RetVal+=Piece.substring(Piece.length-2);
Piece="00"+this.Day;
RetVal+=Piece.substring(Piece.length-2);
return RetVal;
}
return "Invalid NewDate toString() selector.";
}
</script>
<body>
<script type="text/javascript">
var SomeDay=new NewDate(2011,12,25); // Christmas 2011
alert("Date toString(): "+SomeDay.toString());
alert("ISO 8601 Date: "+SomeDay.toString(1));
alert("IMA Date: "+SomeDay.toString(2));
</script>
</body>
</html>
***** End of Experimental Code *****
Sincerely,
Gene Wirchenko