The problem is that eval does not inherit the global scope, so foo is created in a different scope than where it is called. You can solve this problem by specifying the scope explicitly, either in the function definition, or by going to eval . That is, both of the following snippets work as expected:
new Function("eval('window.foo = function(){ alert() }'); foo()")() new Function("eval('function foo(){ alert() }', this); foo()")()
This behavior seems to be related to strict mode, as explained by the answer "use strict" here; + jQuery.getScript () = script cannot export to the global namespace .
source share