Something fun I just discovered while investigating eval in JS: var foo = (function () {
var n = {};
return function (s) {
return eval(s);
};
})();
foo("n.foo = 5");
foo("n.foo + 10");
Okay, what's going on here is, we've created a local variable "n" which is an object. We then return a function that evals things in its lexical environment. Which means that the two calls to "foo" work.This means you can create a private namespace that is inaccessible to outside code, and then return a function that lets you evaluate things in that namespace. This is super awesome because I'm working on getting Nulan to compile to JS, and obviously the only way to currently do that is to use eval. This gives me more flexibility in how to do the eval. In particular, I no longer need to use the global scope: Nulan can operate entirely with lexical scope without needing to wrap everything in (function () { ... })() |