Created
January 26, 2015 20:03
-
-
Save abhiaiyer91/18b539c5858876e6476e to your computer and use it in GitHub Desktop.
encapsulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Rectangle(h,w) { | |
var width=w; // Both the 'width' and 'w' is private | |
var heigth=h; // Both the 'height' and 'h' is private | |
this.setWidth= function(w){ width=w;} | |
this.setHeight= function(h){ heigth=h;} | |
this.getWidth= function(){ return width;} | |
this.getHeight= function(){ return heigth;} | |
this.constructor.prototype.getDiagonal=function() { | |
return Math.sqrt(heigth*heigth+width*width); | |
}; | |
} | |
Rectangle.prototype.getArea=function() { | |
// We must use accessors in a prototype kind of method, | |
// then these methods can not access the private members | |
// of a created object. | |
return this.getWidth()*this.getHeight(); | |
}; | |
var rect = new Rectangle(60,70); | |
rect.setHeight(20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment