I am working on JavaScript architecture with requireJS module.
Module definition is pretty simple:
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();
}
};
});
});
Than I have a class called ‘Module’, inside which I am trying to apply try catch block to module methods like this:
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);
}
}
}
The problem is that as module instance holds only public methods, I cannot apply try catch blocks to private methods.
I am thinking if I make all instance methods public it’s not going to be secure anymore.
Is there’s a way to apply try catch block to private methods as well without reworking each module itself?
Unless you want to add code inside of the module to automatically add a try-catch, then no, there isn’t a way to do it.
You basically have two options:
I wouldn’t let “security” affect your decision. You’re writing JavaScript, people can always view source and see what’s going on, it will never be truly secure.