Mega Code Archive

 
Categories / JavaScript Tutorial / Object Oriented
 

Custom objects and associative object arrays

< HTML> <HEAD>    <TITLE>Custom objects and associative object arrays</TITLE> <SCRIPT> function showProperties (theObject){    for (i in theObject) {       if (theObject[i] != null) {           document.write(i + " : " + theObject[i] + "<br>");                   }       else {          document.write(i + "<br>");       }    }    return; } </SCRIPT> </HEAD> <BODY> <SCRIPT> // constructor function function Rectangle(height, width){    this.height =  height;    this.width = width; } function calc_Area () {    return this.height * this.width; } // turn the function into an object method Rectangle.prototype.calcArea = calc_Area; var theRectangle = new Rectangle (3, 5); theRectangle.width = 10; showProperties (theRectangle); </SCRIPT> </BODY> </HTML>