Constructs and Prototypes

[Total: 0    Average: 0/5]

This file shows how to set up a simple constructor with some default prototype values/functions in them all.

Body Code:

<script language=JavaScript>
//Construct Begin
  function CircleRadius() { return this.r}
  function Circle(x,y,r) {
    this.x=x
	this.y=y
	this.r=r
    this.radius = CircleRadius
  }
  
  // Create and discard an initial object of this type.
  //  This forces NS 3 to create the prototype object
  new Circle(0,0,0)
  
  
  Circle.prototype.pi = 3.14159
  function CircleCircumference() {return 2 * this.pi * this.r}
  Circle.prototype.circumference = CircleCircumference
  Circle.prototype.area = new Function("return this.pi * this.r * this.r")
//Construct End

//Create an instance of the Construct "Circle"
  var c = new Circle(0.0,0.0,1.0)
  var a = c.area()
  var p = c.circumference()
  
  document.write("Circle (radius="+c.radius()+") has an area="+c.area()+" and a cir="+c.circumference()+"<BR>");
</script>

Times Viewed: 1

Leave a Reply

Your email address will not be published. Required fields are marked *