Last active
May 12, 2017 17:09
-
-
Save taufik-nurrohman/a22df22aa749a750497879ba0597c3a7 to your computer and use it in GitHub Desktop.
JavaScript Module Pattern
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
/*! JavaScript Module Pattern by Taufik Nurrohman <https://github.com/tovic> */ | |
(function(win, doc, NS) { | |
(function($) { | |
// module version | |
$.version = '1.0.0'; | |
// collect all instance(s) | |
$.__instance__ = {}; | |
// plug to all instance(s) | |
$.each = function(fn, t) { | |
return setTimeout(function() { | |
var ins = $.__instance__, i; | |
for (i in ins) { | |
fn(ins[i], i, ins); | |
} | |
}, t === 0 ? 0 : (t || 1)), $; | |
}; | |
})(win[NS] = function(target, config) { | |
var $ = this; | |
// return a new instance if `MyModule` was called without the `new` operator | |
if (!($ instanceof win[NS])) { | |
return new win[NS](target, config); | |
} | |
// access module instance from `this` scope with `this.MyModule` | |
target[NS] = $; | |
// store module instance to `MyModule.__instance__` | |
win[NS].__instance__[target.id || target.name || Object.keys(win[NS].__instance__).length] = $; | |
// access module target with `$.target` | |
$.target = target; | |
// create some method(s) … | |
$.set = function() { | |
// ... | |
return $; // make chainable … | |
}; | |
$.get = function() { | |
// ... | |
return $; // make chainable … | |
}; | |
$.reset = function() { | |
// ... | |
return $; // make chainable … | |
}; | |
// return the global object | |
return $; | |
}); | |
})(window, document, 'MyModule'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I started to implement this pattern on all of my JavaScript project because this pattern allows me to add additional functionality without having to access the variable of the instance with this:
Inspired by that
CKEDITOR.instances
object. By default, the instance scrapper will set a new item inMyModule.__instance__
with a key generated by the target ID or target name or target index in the page.