I have the following so-called Revealing Module Pattern and I want to call the function a inside function b using a variable. How can
The problem is that a
is not a member, but a variable (and it should be a local one!). You cannot access those dynamically by name unless you use dark magic (eval
).
You will need to make it a member of an object, so that you can access it by bracket notation:
var foo = (function() {
var members = {
a: function() { }
};
function b() {
var memberName = 'a';
members[memberName].call();
}
return {b: b};
}());
foo = function() {
var inner = {
a : function() {
}
};
b = function() {
memberName = 'a';
// Call a() using value stored in variable `memberName`.
inner[memberName]();
}
return {b: b};
}();