Last active
August 29, 2015 14:09
-
-
Save shaunwallace/468bcf7404963ff9f9e3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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