Ответ 1
Просто используйте arguments
:
require(amd_modules, function() {
console.log("All modules loaded");
// arguments should now be an array of your required modules
// in the same order you required them
});
Однако, если у вас нет оснований для этого, вы, вероятно, захотите переосмыслить то, как вы разрабатываете приложение, даже на самом верхнем уровне ваши модули должны быть простыми и проверенными. Наличие большого числа зависимостей указывает на то, что вы, вероятно, пытаетесь сделать многое в своей функции обратного вызова. Разбейте каждый путь кода на свой собственный модуль, а затем переключитесь только на свою зависимость верхнего уровня. В коде:
// Instead of this:
require(amd_modules, function() {
console.log("All modules loaded");
if (complex_condition_A) {
var x = arguments[0],
y = arguments[1],
z = arguments[2];
// Do things with x, y and z
}
else if (complex_condition_B) {
var a = arguments[0],
b = arguments[1];
// Do things with a and b
}
else {
// et cetera, et cetera, et cetera
}
});
// Do this instead
var rootModule;
if (complex_condition_A) rootModule = "A";
else if (complex_condition_B) rootModule = "B";
else rootModule = "C";
require(rootModule, function(root) {
// Root has the same API, regardless of which implementation it is
// This might be as simple as an `init` method that does everything
// or as complex as, say Facebook API, but with different libraries
// loaded depending on the platform the page is loaded on
// (IE vs. Android for example).
});