Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
Last active August 29, 2015 14:09
Show Gist options
  • Save shaunwallace/468bcf7404963ff9f9e3 to your computer and use it in GitHub Desktop.
Save shaunwallace/468bcf7404963ff9f9e3 to your computer and use it in GitHub Desktop.
var PublicShape = function() {
// public members
this.width = 50;
this.height = 50;
};
PublicShape.prototype.getWidth = function() {
// public method with priveledged access to the private members
return this.width;
}
var publicShape = new PublicShape();
console.log( publicShape.width ); // will output 50
console.log( publicShape.getWidth() ); // will output 50
var PrivateShape = function() {
// private members
var width = 100
, height = 100;
// i am public and now a property on the global object
// because I did not declare myself using the keyword var
length = 300;
// public method with priveledged access to the private members
this.getWidth = function() {
return width;
};
};
var privateShape = new PrivateShape();
console.log( privateShape.width ); // will output undefined
console.log( privateShape.getWidth() ); // will output 50
console.log( privateShape.length ) // will output undefined
console.log( window.length ) // will output 300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment