Skip to content

Instantly share code, notes, and snippets.

@kasper
Last active October 12, 2015 15:57
Show Gist options
  • Save kasper/4050837 to your computer and use it in GitHub Desktop.
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)
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