Created
September 10, 2012 18:25
-
-
Save asciidisco/3692732 to your computer and use it in GitHub Desktop.
"Privates" in JS
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
// Defining "private" properties in JavaScript | |
// define youre own scope by using an | |
// immediate executed function | |
(function (){ | |
// x is only be visible within this scope a.k.a this function | |
var x = null; | |
var Bla = function(blup) { | |
// x is visible here | |
x = blup; | |
}; | |
Bla.prototype.on = function() { | |
// x is visible here | |
alert(x); | |
}; | |
}()); | |
// x is not visible here | |
console.log(typeof x === 'undefined'); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment