Skip to content

Instantly share code, notes, and snippets.

@A
Last active December 17, 2015 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A/5691071 to your computer and use it in GitHub Desktop.
Save A/5691071 to your computer and use it in GitHub Desktop.
Пришел к такой структуре js-модулей. Создаю инстанс так new MyModule({ // Здесь передаю параметры });
/* global shortcodeParams */
/* exported MyModule */
var MyModule = function (params) {
'use strict';
var defaults = {
};
var init = function () {
initParams();
// ...init other stuff here...
};
var initParams = function () {
params = shortcodeParams(defaults, params);
};
};
/* exported shortcodeParams */
/**
* Combine user attributes with known attributes and fill in defaults when needed.
*
* The defaults should be considered to be all of the attributes which are
* supported by the caller and given as a list. The returned attributes will
* only contain the attributes in the $pairs list.
*
* If the params list has unsupported attributes, then they will be ignored and
* removed from the final returned list.
*
*
* @param array defaults Entire list of supported attributes and their defaults.
* @param array params User defined attributes in shortcode tag.
* @return array Combined and filtered attribute list.
*/
var shortcodeParams = function (defaults, params) {
'use strict';
if (typeof params !== 'object') { return defaults; }
var out = {};
for (var key in defaults) {
if (defaults.hasOwnProperty(key)) {
if (params[key]) {
out[key] = params[key];
} else {
out[key] = defaults[key];
}
}
}
return out;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment