私は requireJS モジュールを使用して JavaScript アーキテクチャに取り組んでいます。モジュールの定義は非常に単純です:
define([
"dependency1.js",
"dependency2.js"
], function (dep1, dep2) {
dep1.dropdown = new Module("dropdown", function (sandbox) {
// Private functions
function getWhatever() {
// do something
}
function getAnother() {
// do another thing
}
// Public methods
return {
doSomething: function () {
// do one more thing
getAnother();
}
};
});
});
「モジュール」と呼ばれるクラスがあり、その中で次のようにモジュールメソッドにtry catchブロックを適用しようとしています:
var Module = function (id, creator) {
var instance,
sandbox = buildSandbox(),
name,
method;
instance = creator(sandbox);
for (name in instance) {
// Looping though all methods inside module instance
method = instance[name];
if (typeof method === "function") {
// Making every function execute within try catch block
instance[name] = (function (name, method) {
return function () {
try { return method.apply(this, arguments); }
catch (ex) { console.log("ERROR", name + "(): " + ex.message); }
};
})(name, method);
}
}
}
問題は、モジュールインスタンスがパブリック メソッドしか保持していないため、try catchブロックをプライベート メソッドに適用できないことです。
すべてのインスタンスメソッドを公開すると、安全ではなくなると考えています。
各モジュール自体を作り直すことなく、プライベート メソッドにもtry catchブロックを適用する方法はありますか?