// 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. function Circle(radius) { // the constructor defines the class itself // r is an instance variable; defined and initialized in the constructor this.r =index.html radius; } // Circle.PI is a class variable--it is a property of the constructor function Circle.PI = 3.14159; // Here is a function that computes a circle area. function Circle_area() { return Circle.PI * this.r * this.r; } // Here we make the function into an instance method by assigning it // to the prototype object of the constructor. Remember that we have to // create and discard one object before the prototype object exists new Circle(0); Circle.prototype.area = Circle_area; // Here's another function. It takes two circle objects are arguments and // returns the one that is larger (has the larger radius). function Circle_max(a,b) { if (a.r > b.r) return a; else return b; } // Since this function compares two circle objects, it doesn't make sense as // an instance method operating on a single circle object. But we don't want // it to be a stand-alone function either, so we make it into a class method // by assigning it to the constructor function: Circle.max = Circle_max; // Here is some code that uses each of these fields: c = new Circle(1.0); // create an instance of the Circle class c.r = 2.2; // set the r instance variable a = c.area(); // invoke the area() instance method x = Math.exp(Circle.PI); // use the PI class variable in our own computation. d = new Circle(1.2); // create another Circle instance bigger = Circle.max(c,d); // use the max() class method.
file: /Techref/language/java/script/definitive/examples/7.5.txt, 1KB, , updated: 1997/4/4 11:59, local time: 2024/8/22 22:22,
3.146.152.196:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://piclist.com/tecHREF/language/java/script/definitive/examples/7.5.txt"> language java script definitive examples 7</A> |
Did you find what you needed? |
![]() o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Welcome to piclist.com! |
.