Skip to content

Instantly share code, notes, and snippets.

@thgaskell

thgaskell/Class.js

Created Nov 13, 2013
Embed
What would you like to do?
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