// This example is from the book _JavaScript: The Definitive Guide_. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // This example is provided WITHOUT WARRANTY either expressed or implied. // You may study, use, modify, and distribute it for any purpose. // Define a constructor method for our class. // Use it to initialize properties that will be different for // each individual circle object. function Circle(x, y, r) { this.x =index.html x; // the X coordinate of the center of the circle this.y = y; // the Y coordinate of the center of the circle this.r = r; // the radius of the circle } // Create and discard an initial Circle object. // Doing this forces the prototype object to be created new Circle(0,0,0); // Now define a constant; a property that will be shared by // all circle objects. Actually, we could just use Math.PI, // but we do it this way for the sake of example. Circle.prototype.pi = 3.14159; // Now define some functions that perform computations on circles // Note the use of the constant defined above function Circle_circumference() { return 2 * this.pi * this.r; } function Circle_area() { return this.pi * this.r * this.r; } // Make these functions into methods of all Circle objects by // setting them as properties of the prototype object. Circle.prototype.circumference = Circle_circumference; Circle.prototype.area = Circle_area; // Now define a default property. Most Circle objects will share this // default value, but some may override it by setting creating their // own unshared copy of the property. Circle.prototype.url = "images/default_circle.gif"; // Now, create a circle object, and use the methods defined // by the prototype object c = new Circle(0.0, 0.0, 1.0); a = c.area(); p = c.circumference();