Adding method to a class using prototype : Member Function « Object Oriented « JavaScript Tutorial






<HTML>
<HEAD>
<TITLE>Instance method demo</TITLE>
</HEAD>
   <BODY>
   <H1>
   <SCRIPT>
   // constructor function
   function Rectangle(height, width){
      this.height =  height;
      this.width = width;
   }
  
   function getArea () {
      return this.height * this.width;
   }
   // turn the function into an object method
   Rectangle.prototype.calcArea = getArea;

   var theRectangle = new Rectangle (3, 5);
   theRectangle.width = 10;

   document.write("The rectangle instance height is: " + theRectangle.height + "<br>");
   document.write("The rectangle instance width is: " + theRectangle.width  + "<br>");
   document.write ("The calcArea method returns: " + theRectangle.calcArea());
   </SCRIPT>
   </H1>
   </BODY>
</HTML>








25.4.Member Function
25.4.1.Defining the object methods outside of the factory functions and then pointing to them
25.4.2.Create an Object with member method
25.4.3.Add member function
25.4.4.Adding method to a class using prototype
25.4.5.Custom objects and associative object arrays
25.4.6.Instance method
25.4.7.Call object member function