Skip to content

Instantly share code, notes, and snippets.

@enijar
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enijar/f12c343b4582f1d4b369 to your computer and use it in GitHub Desktop.
Save enijar/f12c343b4582f1d4b369 to your computer and use it in GitHub Desktop.
Object-Oriented JavaScript
/**
* Cross-browser supported OOP JS class with methods
*/
// Create a class "Config". The self-invoking
// function avoids the pollution the global namespace
var Config = (function() {
// Construct
function Config() {}
// Add a method "update" (that's what the prototype is for!)
Config.prototype.update = function(obj) {
this.settings = obj;
};
// Return only the function, every other local variable stays
// in this scope
return Config;
})();
// Instantiate class
var config = new Config();
// Class a method "update"
config.update({
debug: false,
windowWidth: $(window).width(),
windowHeight: $(window).height()
});
console.log(config.settings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment