Looking for a JavaScript function f
that can take an anonymous function g
and a name n
, and assign g
to that name in the global scope or current scope. The function should be usable in this way:
f(function(){ /* code */ }, "foo");
foo(); // this call should now work!
Is there a way to achieve this using only JavaScript, without involving any DOM manipulation? This code is not meant for browser execution.
Note: Whether or not there is a valid reason for this functionality is irrelevant. Please refrain from discussing the importance of maintaining a clean global scope.