Re:A spreadsheet in fewer than 30 lines of JavaScript, no li...
by agumonkey on Wednesday November 13, 2013 @10:41AM
(#6725866)
with (context) + eval = user-space lexical environment ?
[ Parent
| on HN ]
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li...
by curveship on Wednesday November 13, 2013 @02:46PM
(#6727922)
No, unfortunately. If a reference isn't found in your context object, it falls through to the ambient namespace. So say you run ...
context = { foo: 1, bar: 2 };
with (context) {
foo = 2;
bar = 3;
}
That will probably do what you expect. But now say you run it where context is just ... context = { foo: 1 };
with (context) {
foo = 2;
bar = 3;
}
... you might think this would add a 'bar' property to your context object, but in fact it creates a global variable bar. Whoops!In short, 'with' is pointy on both ends.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li...
by agumonkey on Wednesday November 13, 2013 @03:04PM
(#6728062)
Oh yeah, `with` is dangerous.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li...
by richthegeek on Wednesday November 13, 2013 @11:36AM
(#6726345)
Yep, I'm using something very like this in a Node project I'm working on. However, with(context) has some pretty nasty side effects that I just couldn't sidestep, so I used the "contextify" module.
If you're needing with and have access to npm, use contextify, It'll save you a lot of effort!
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li...
by agumonkey on Wednesday November 13, 2013 @12:12PM
(#6726592)
Thanks for the tip, but I'm far from working with javascript right now[1], I was barely trying to understand the trick. I love the fact that this 'app' is a 6x6 variables grid layout javascript editor in disguise.
[1] I just finished skimming through eloquent js and js allonge, and this still eluded me.