Last active
August 29, 2015 14:08
-
-
Save rochnyak-d-i/51a1811807c26638e210 to your computer and use it in GitHub Desktop.
JS Зависимости
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
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