Last active
October 12, 2015 15:57
-
-
Save kasper/4050837 to your computer and use it in GitHub Desktop.
Encapsulated JS Objects with Module Pattern (with static variables across instances of modules)
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 Foobar = (function () { | |
/* Static variables across instances of modules */ | |
var _FOO = 'foo'; | |
// Constructor | |
var module = function (foo) { | |
/* Private variables */ | |
var _foo = foo || 'default'; | |
/* Private functions */ | |
function magic() { | |
_foo = 'magic'; | |
} | |
/* Public functions (__proto__ for static) */ | |
this.__proto__.getFOO = function () { | |
return _FOO; | |
} | |
this.__proto__.setFOO = function (FOO) { | |
_FOO = FOO; | |
} | |
this.getFoo = function () { | |
return _foo; | |
} | |
this.setFoo = function (foo) { | |
_foo = foo; | |
magic(); | |
} | |
} | |
return module; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment