Skip to content

Instantly share code, notes, and snippets.

@rochnyak-d-i
Last active August 29, 2015 14:08
Show Gist options
  • Save rochnyak-d-i/51a1811807c26638e210 to your computer and use it in GitHub Desktop.
Save rochnyak-d-i/51a1811807c26638e210 to your computer and use it in GitHub Desktop.
JS Зависимости
function Sandbox() {
// преобразовать аргументы в массив
var
args = Array.prototype.slice.call(arguments)
// последний аргумент ­ функция обратного вызова
, callback = args.pop()
// имена модулей могут передаваться в форме массива
// или в виде отдельных параметров
, modules = (args[0] && typeof args[0] === 'string') ? args : args[0]
, i
;
// проверить, была ли функция вызвана как конструктор
if (!(this instanceof Sandbox)) {
return new Sandbox(modules, callback);
}
// добавить модули в базовый объект `this`
// отсутствие аргументов с именами модулей или аргумент со значением “*”
// предполагает необходимость включения “всех модулей”
if (!modules || modules === '*') {
modules = [];
for (i in Sandbox.modules) {
if (Sandbox.modules.hasOwnProperty(i)) {
modules.push(i);
}
}
}
// инициализировать необходимые модули
for (i = 0; i < modules.length; i += 1) {
Sandbox.modules[modules[i]](this);
}
// вызвать функцию обратного вызова
callback(this);
}
// добавить свойства к прототипу, если это необходимо
Sandbox.prototype = {
name: 'My Application'
, version: '1.0'
, getName: function () {
return this.name;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment