単に使用します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's API, but with different libraries
// loaded depending on the platform the page is loaded on
// (IE vs. Android for example).
});