code detail question.

1 view
Skip to first unread message

Johnny

unread,
Aug 13, 2008, 2:12:43 AM8/13/08
to Chiron JavaScript
hello, i just learn about the source code of <modules.js> , and i am
confusing with a lot of them ^_^. maybe someone which have enough time
could explain it for me ?thanks:

1.the first structure:

(
function(evalGlobal)
{
...
}
)
(
function()
{

return eval.call(window, arguments[0]);
}
);

the second anonymous function doesn't take arguments in definetion,
but the code in it would like to receive arguments[0]

the second anonymous functon return the execute result of eval , but
what exactlly does the eval return for ? i allways get the "
undefined"

so i want to learn that does the code above is equal to the code
below:

(
function(evalGlobal)
{
...
evalGlobal(someCodeTextToRegisterInWindowScope)
}
)
(
function(willBeEvaledCode)
{

eval.call(window, willBeEvaledCode);
}
);

what subtlety in it ?

2. eh~~~~ i just haven't learn a lot of it yet ,so how about solve my
first problem first?thanks very much.

Kris Kowal

unread,
Aug 13, 2008, 3:46:13 PM8/13/08
to chir...@googlegroups.com
Hey Johnny. First, JavaScript functions can receive any number of
arguments regardless of the number of declared arguments; this makes
it possible to do "variadic" arguments in JavaScript, where you can
iterate on an arbitrary number of passed arguments like the
|Array.prototype.push| function or the more universally familiar C
|printf| function.

Regarding |evalGlobal|, when I talked about this with Mike Samuel from
the Google Caja team, he called it "Variable Laundering", drawing an
analogy to money laundering, where you make the source of money
untraceable. The idea is to muck up the scopes by introducing any
names into the evaled code's scope chain. By passing arguments into
evalGlobal variadically (through the arguments object), I don't
introduce a |willBeEvaledCode|, or simply |program|, variable into the
scope chain. This means that you're free to declare a global variable
with the same name and access it in any program run through
|evalGlobal|.

This the same reason for declaring |evalGlobal| in the arguments of
"module pattern" "enclosure". Since I declared the function in global
scope and passed it into the enclosure to be bound as the |evalGlobal|
argument variable, you are also free to declare an |evalGlobal|
function variable in global scope and use it in programs run through
<modules.js>'s |evalGlobal|.

As for returning the return value of |eval|, this allows the return
value of |evalGlobal| to mimic the specified return value of |eval|.
This is supposed to be the value of the last run statement in the
program. This feature is often used to insecurely but quickly parse
JSON from XHRs since it's a strict subset of JavaScript. I later use
|evalGlobal| in <json.js>.

Thanks for asking these questions; it's my pleasure to illuminate this
portion of the code.

Kris

Johnny

unread,
Aug 14, 2008, 4:33:26 AM8/14/08
to Chiron JavaScript
so i am not quite very flow your details ,but thanks, and i have no
money to Laundering... hehe.
> > first problem first?thanks very much.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages