In what ways does the following "eval" regularity break?

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Thu Oct 30 14:08:11 PDT 2008


Mark S. Miller wrote:
> In a similar vein, in what ways might
> 
>     new Function("<some code b>")
> differ in meaning from
>     function(){eval("<some code b>");}
> 
> The obvious one: In the latter, <source code b> can refer to
> non-global variable that are in scope at eval's site. What else?

Note that those variables at eval's call site might include some
that shadow global bindings, including Array, Object, etc. Either
the global or the shadowing versions of those bindings might not
be the original ones.


Another, non-semantic but nevertheless quite important, difference
is efficiency: a function created by 'new Function' will typically
hold an intermediate or compiled form of the function code that
is not recomputed if the function is reused.

(Ideally the underlying primitive should be 'compile(code, n)'
where n is a hint to the number of times the code is likely to be
called.)

-- 
David-Sarah Hopwood


More information about the Es-discuss mailing list