Click to See Complete Forum and Search --> : How does this function work?


ekta
11-07-2003, 01:49 PM
Hi All:

I am very new to JavaScript. I want to know what this function is doing? I would really appreciate if anyone can explain me as to what is goin on over here.

function MyDate( year, month, day )
{
var yy = parseInt( 0+year, 10 );
if ( yy < 50 )
this.year = 2000 + yy;
else
this.year = 1900 + yy;

this.month = parseInt( 0+month, 10 );
this.day = parseInt( 0+day, 10 );
}

Thanx,

Ekta

Charles
11-07-2003, 02:26 PM
That's no ordinary function, that's an Object constructor for an MyDate class. One would use it thusly...

myDate = new MyDate (1964, 5, 9)

To be usefulm, though we would need to define some class method would be inherited by all members of the class...

MyDate.prototype.toString = function () {return [this.year, this.month, this.day].join('/')}
myDate = new MyDate (1964, 5, 9);
alert (myDate);


or we could give one member of the class its own method...

MyDate.prototype.toString = function () {return [this.year, this.month, this.day].join('/')}
myDate = new MyDate (1964, 5, 9);
myDate.toString = function () {return 'The day of my birth.'}
alert (myDate);


Note that the use of capitals is intentional. See http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html.

ekta
11-07-2003, 03:13 PM
Thanx for replying Charles. Excuse my ignorance but I still dont understand what does function is doing?

Charles
11-07-2003, 03:38 PM
See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/obj.html.