Skip to content

Instantly share code, notes, and snippets.

@benhowdle89
Created July 22, 2012 11:09
Show Gist options
  • Save benhowdle89/3159306 to your computer and use it in GitHub Desktop.
Save benhowdle89/3159306 to your computer and use it in GitHub Desktop.
A base class for JavaScript projects
​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
@jackfranklin
Copy link

this.fname = (fname !== undefined) ? fname : 'guest';

Can be done as:

this.fname = fname || "guest";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment