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



Daniel Smedegaard Buus <daniel@xxxxxxxxxxxxxxxxxxxxxxx> writes:

Consider this piece of code:

eval(" function f0825022001228918765(args)
{$.get('/xhr/video_player_comments_box.php',merge_objects({},
{\"category_id\":\"1\",\"clip_id\":\"371\"}, args),function(response){var
old =
$('#video_player_meta_box');old.hide().after(response);old.remove();});};
function f0825246001228918765(){ajaxify_form({
form_element_id:'commenting_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
\"clip_id\":\"371\"

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).

/L
--
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 ...
    (comp.lang.javascript)
  • 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" ...
    (comp.lang.javascript)
  • 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. ...
    (comp.lang.javascript)
  • 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 ...
    (comp.lang.javascript)
  • 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: ...
    (comp.lang.javascript)