1 <HTML> 2 <HEAD> 3 <TITLE>JavaScript Example: Math Computation</TITLE> 4 5 <SCRIPT LANGUAGE="JavaScript1.1"> 6 <!-- hide script from old browsers 7 8 // Circle constructor: 9 function Circle() { 10 if ( arguments.length > 0 ) { 11 this.r = Circle.arguments[0]; 12 } else { 13 this.r = null; 14 } 15 this.getRadius = new Function( "return this.r" ); 16 this.setRadius = new Function( "r", "this.r = r" ); 17 } 18 19 // Method to compute area of a circle: 20 function Circle_area() { 21 return Math.PI * this.r * this.r; 22 } 23 24 // Create a dummy Circle object: 25 new Circle(); 26 27 // Add method to Circle object: 28 Circle.prototype.area = Circle_area; 29 30 // Compute area and update form: 31 function areaOfCircle( form ) { 32 var c = new Circle(); 33 c.setRadius( form.radius.value ); 34 form.output.value = c.area(); 35 } 36 37 // end script hiding --> 38 </SCRIPT> 39 40 </HEAD> 41 42 <BODY BGCOLOR="#FFFFFF"> 43 44 <H2>Mathematical Operations</H2> 45 46 Demonstration of mathematical methods:<p> 47 48 <FORM> 49 50 Enter a value for the radius:<P> 51 <INPUT TYPE="text" NAME="radius" VALUE = "10"> 52 <INPUT TYPE="button" VALUE="Find Area" 53 onClick="areaOfCircle(this.form)"> 54 55 <P> 56 Area of a circle with this radius is 57 <INPUT TYPE="text" NAME="output" SIZE=20> 58 59 </FORM> 60 61 <P> <HR> <P> 62 63 Note: The area of a circle with radius r 64 is PI * r^2 <P> 65 66 </BODY> 67 </HTML>