This code works, but would it be possible (in theory) to call b() if the name is inside a string (i.e. dynamic)?
No, function declaration has the same rules for scope, so it's not possible even in theory (unless we talk about closures, of course).
If b() would be declared in the global scope, we could use windowstringContainingName; Is there a possibility in this case?
Yes, of course: this...
(function a() {
// Nested function
window.b = function() {
console.log("Works!");
}
b();
})();
b(); // or window['b'](), or window.b()
... will log 'Works'
twice. I used explicit global object here, as in the strict mode direct assignment to b
without declaring it (using b = function() ...
instead of window.b = function() ...
) will cause ReferenceError.