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'