Skip to content

Instantly share code, notes, and snippets.

@abhiaiyer91
Created January 26, 2015 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhiaiyer91/18b539c5858876e6476e to your computer and use it in GitHub Desktop.
Save abhiaiyer91/18b539c5858876e6476e to your computer and use it in GitHub Desktop.
encapsulation
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