# Posted By

mlange on 10/03/06

# Statistics

Viewed 476 times
Favorited by 1 user(s)

# Creating objects

/ Published in: JavaScript
Save to your folder(s)

Copy this code and paste it in your HTML
`// --------------------------------------------------//    Creating an object with methods function mycircle(x,y,r) {  this.xcoord = x;  this.ycoord = y;  this.radius = r;  this.retArea = getTheArea;  //This next line uses an alternative syntax  this.retCirc = function () { return ( Math.PI * this.radius * 2 ); };  this.mvBy = mvCclBy;}function getTheArea() {  return ( Math.PI * this.radius * this.radius );}function mvCclBy(xDis,yDis) {  this.xcoord += xDis;  this.ycoord += yDis;} /*create a mycircle called testcircle where testcircle.xcoord is 3and testcircle.ycoord is 4 and testcircle.radius is 5*/var testcircle = new mycircle(3,4,5);/*use the mvBy method to displace the centre of testcircle.move it by 2 in the x direction and 3 in the y direction*/testcircle.mvBy(2,3);//testcircle.xcoord is now 5 and testcircle.ycoord is now 7 window.alert( 'The area of the circle is ' + testcircle.retArea() );window.alert( 'The circumference is ' + testcircle.retCirc() );  // --------------------------------------------------//    Advanced object techniques testcircle.texture = 'smooth';mycircle.prototype.texture = 'smooth';  // all of the mycircles that we have created will now inherit the new property: alert(testcircle.texture);//alerts 'smooth' mycircle.prototype.setArea = function (oArea) {  this.radius = Math.sqrt( oArea / Math.PI );};mycircle.setArea(5);  function mycircle(x,y,r) {  this.xcoord = x;  this.ycoord = y;  this.radius = r;}mycircle.prototype.retArea = function () {  return ( Math.PI * this.radius * this.radius );};mycircle.prototype.retCirc = function () {  return ( Math.PI * this.radius * 2 );};mycircle.prototype.mvBy = function (xDis,yDis) {  this.xcoord += xDis;  this.ycoord += yDis;};  // --------------------------------------------------//    Sub-classes and class inheritance /*This effectively makes mysphere a sub-class of mycircle, and making the mysphere class constructor inherit from the mycircle class constructor is as simple as this:*/function mysphere(x,y,z,r) { ... constructor code ... }mysphere.prototype = new mycircle(); ------------ function mycircle(x,y,r) {  if( arguments.length ) { this.getready(x,y,r); }}mycircle.prototype.getready = function (a,b,c) {  this.xcoord = a;  this.ycoord = b;  this.radius = c;};mycircle.prototype.retArea = function () {  return ( Math.PI * this.radius * this.radius );};mycircle.prototype.retCirc = function () {  return ( Math.PI * this.radius * 2 );};mycircle.prototype.mvBy = function (xDis,yDis) {  this.xcoord += xDis; this.ycoord += yDis;}; ------- function mysphere(x,y,z,r) {  if( arguments.length ) { this.getready(x,y,z,r); }}//inherit from the mycircle prototypemysphere.prototype = new mycircle();//put the correct constructor reference back (not essential)mysphere.prototype.constructor = mysphere; mysphere.prototype.getready = function (a,b,c,d) {  //reference the getready method from the parent class  this.tempReady = mycircle.prototype.getready;  //and run it as if it were part of this object  this.tempReady(a,b,d);  //now that all required properties have been inherited  //from the parent class, define extra ones from this class  this.zcoord = c;}mysphere.prototype.mvBy = function (xDis,yDis,zDis) {  //override the existing method  this.xcoord += xDis;  this.ycoord += yDis;  this.zcoord += zDis;};mysphere.prototype.retVol = function () {  return ( 4 / 3 ) * Math.PI * Math.pow( this.radius, 3 );};mysphere.prototype.retSurf = function () {  return 4 * Math.PI * this.radius * this.radius;}; -------- //  to use itvar testsphere = new mysphere(3,4,5,6); alert( 'The cross-section area is ' + testsphere.retArea() );alert( 'The circumference is ' + testsphere.retCirc() );alert( 'The volume is ' + testsphere.retVol() );alert( 'The surface area is ' + testsphere.retSurf() );`