Mochabot log - CommonJS IRC channel: #commonjs on irc.freenode.net

2010-04-10:

[10:02] <fholm> Anyone around who can explain why this snippet: http://paste2.org/p/766396 produces: undefined, 1, g2, undefined. While this snippet: http://paste2.org/p/766397 crashes with "bar" not defined.
[10:02] <fholm> Uhm, second snippet should be: http://paste2.org/p/766399
[10:03] <fholm> from my point of view both would crash from "bar" not being defined on the first alert() since bar only exists inside the with()-statement
[10:03] <fholm> but it seems as if firefox/chrome defines all variables by their "master" scope (global/function) and if I create a variable inside a with() statement it still "exists" outside it, it's just undefined.
[10:15] <keeto> fholm: yep.. variable declaration is based on the execution context.
[10:16] <keeto> with only deals with the scope chain; variable instantiation is still the same.
[10:17] <fholm> http://stackoverflow.com/questions/2613014/behavior-of-local-variables-in-javascripts-with-statement
[10:18] <fholm> keeto: read that
[10:18] <fholm> keeto: but if that is the case, why is it "reset" to undefined when it leaves the with() statement
[10:19] <fholm> and if "bar" actually was the same variable both outside and inside the with()-statement, then the last example zoo() would print "undefined"
[10:20] <keeto> after your alert(zoo()); could you add, alert(foo.bar) ?
[10:21] <fholm> oh so it creates a variable in the execution context
[10:21] <fholm> but "bar" always refers to the with() objects bar?
[10:22] <fholm> (foo.bar prints g2)
[10:23] <keeto> that's because of variable instantiation.
[10:23] <keeto> http://pastie.textmate.org/private/zxpifjaf0lqbtytcslv1zg
[10:23] <keeto> that's how your code actually comes out.
[10:23] <fholm> yeah, figured as much now
[10:23] <fholm> wow that is retarded.
[10:23] <fholm> (but a lot easier to implement in my runtime, so yay me)
[10:23] <keeto> :)
[10:25] <fholm> thanks, btw.
[10:26] <keeto> np. :)

 

 

Logs by date :