Can someone help me understand the following behavior? I'd expect that, since I can set the global f from inside this callback, I should also be able to change it. I don't understand well enough how node handles context vs. global in the REPL to make sense of this, and I'd appreciate any insight.
Start a REPL without useGlobal
$ cat test.js
var repl = require('repl');
repl.start({useGlobal:false});
Now try setting f twice in a row:
$ node test.js
> f
ReferenceError: f is not defined
> setTimeout(function(){f=1;}, 0);
> f
1
Works the first time... now try again:
> setTimeout(function(){f=2;}, 0);
> f
1
Huh!
The first run sets it; the second doesn't affect it.
(Setting useGlobal:true I get the behavior I expect.)
!function() { f = 2}();works as expected. I've no Idea... maybe a bug?useGlobal if set to true , then the repl will use the global object, instead of running scripts in a separate context.I suppose it has something to do withthis(maybe for a low-level bug)