Re: Scope question... I think...

Daniel Smedegaard Buus <daniel@xxxxxxxxxxxxxxxxxxxxxxx> writes:

Consider this piece of code:

eval(" function f0825022001228918765(args)
{\"category_id\":\"1\",\"clip_id\":\"371\"}, args),function(response){var
old =
function f0825246001228918765(){ajaxify_form({
submit_url:'/json/core_create_clip_comment.php', track_input_states:true,
clear_texts_on_first_focus:true, post_data:{\"category_id\":\"1\";

(Missing comma here!)

\"clip_id\":\"371\"}, on_failure: function(response){
$(\"#info\").removeClass(\"thanksWriteComment_hide\").addClass(\"thanksWriteComment_show\"); }} );};
setTimeout('f0825246001228918765();', 100); ");

Why would this error out with "f0825246001228918765 is not defined"? Is this
a scope issue?

First of all: Why use eval for a fixed string at all? Just remove the
eval and unquote the string and execute it directly. Eval is evil.
And it makes the formatting completely impossible to read. I tried to
reformat it, but it didn't parse because of a missing comma before

Second: Don't use a string as first argument to setTimeout (that's
going to be eval'ed in the global scope at a later point, and ...
eval is evil :)
Instead just give the function itself as first argument to setTimeout.
There were once some browsers that didn't understand a function as
argument, but they should be all dead by now.

This is probably also your problem: Eval evaluates in the current scope,
which might not be the global scope, but the string passed to setTimeout
is evaluated in the global scope. If you use a function argument to
setTimeout, it should work.

Using window.eval instead of just eval will probably be safe eventually,
since many browsers are converging on making that evaluate the string
in the global context (whereas it has so far worked either as real eval,
or thrown an exception).

Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'


Relevant Pages

  • Re: Object problem
    ... If the first argument to setTimeout is a string, ... the code is parsed and executed in the global scope and with "this" ... refers to the global object, but the function closure retains its ...
  • Re: JSLint Reports on last 2 Production Versions of jQuery
    ... The function constructor is safer because it does not use global scope. ... the meaning of the code (when I see strict comparisons, ... it is not clear if x is going to be a string or a String. ... They have lots of useless code and plenty of useless "programmers" ...
  • Re: setTimeout() and string literals - stuck
    ... Any time I see setTimoutcalled with a goofy string expression, ... figure the user is fighting a closure. ... setTimeout() and see in the next call that the new value of i holds. ...
  • Re: (simple) question - function calls without ()
    ... reason for it. ... However setTimeout is being passed ... a string in this case and knows nothing about the ... not part of the actual language and so not in ECMAScript ...
  • Re: Almost got my setTimeout to work
    ... setTimeout function are evaluated when the timeout is up, ... The original form takes a string as its first parameter and evaluates this string when the timeout elapses. ... If you use the string form, setTimeoutwill evaluate the string like eval: ...