Skip to content

Instantly share code, notes, and snippets.

@thgaskell
Created November 13, 2013 02:42
Show Gist options
  • Save thgaskell/7442767 to your computer and use it in GitHub Desktop.
Save thgaskell/7442767 to your computer and use it in GitHub Desktop.
Mimic Java classes using closures and object properties.
var Class =
// Create a closure with an Immediately-Invoked Function Expression (IIFE)
(function () {
(function _init() {
// Similar to a static initializer in Java
// This is optional, but it prevents polluting the Class scope.
})();
// Private static variable
var _static;
// Public static variable
Class.static;
function Class(arguments) {
// Private instance variable
var _instance;
// Public instance variable
this.instance;
}
// Publicly expose the Class constructor
return Class;
})();
// Create an instance of Class
var object = new Class();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment