Created
November 13, 2013 02:42
-
-
Save thgaskell/7442767 to your computer and use it in GitHub Desktop.
Mimic Java classes using closures and object properties.
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 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