Created
July 22, 2012 11:09
-
-
Save benhowdle89/3159306 to your computer and use it in GitHub Desktop.
A base class for JavaScript projects
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
var myClass = function(){ | |
var privateVar = 'do not look at me from the outside'; | |
return { | |
publicVar: 'access me from anywhere', | |
fname: '', | |
init: function(fname){ | |
this.fname = (fname !== undefined) ? fname : 'guest'; | |
console.log('you\'ve kicked this class off, ' + this.fname); | |
}, | |
getPrivateVar: function(){ | |
return privateVar; | |
} | |
} | |
}(); | |
myClass.init(); // you've kicked this class off, guest | |
myClass.init('ben'); // you've kicked this class off, ben | |
console.log(myClass.publicVar); // access me from anywhere | |
console.log(myClass.privateVar); // undefined | |
console.log(myClass.getPrivateVar()); // do not look at me from the outside |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this.fname = (fname !== undefined) ? fname : 'guest';
Can be done as:
this.fname = fname || "guest"
;