[0] http://www.yuiblog.com/blog/2006/04/11/with-statement-consid...
[0] http://www.yuiblog.com/blog/2006/04/11/with-statement-consid...
For with statements in particular, templating and scoped evaluation (I provide an example from the Firebug console in the book) are probably the two areas in which I've seen it most successfully used.
For code evaluation I only have to direct you to the creation of Coffeescript (an entirely evaluated language) whose innovations have directly influenced new versions of the ECMAScript language.
They have their place and when used sparingly, but effectively, the can be formidable parts of a web developer's arsenal.
> For code evaluation I only have to direct you to the creation
> of Coffeescript (an entirely evaluated language) whose
> innovations have directly influenced new versions of the
> ECMAScript language.
The standard way to use CoffeeScript is to compile it to JavaScript first, and then deploy and run with that. There's zero "eval()" involved. Or rather, doing direct "eval()" of CoffeeScript is possible, and on occasion fun, but is discouraged for all the usual reasons.If nothing else, surely it should be avoided in production purely for performance reasons.
So they _can_ be used effectively if one really knows what one is doing, but they're a bit of a footgun.
You can imagine code like:
function () {
with (match("Foo a b")) { return a + b }
with (match("Bar a b")) { return a - b }
}
This would let you have basic pattern matching. Of course, this would also necessitate adding constructors (in the pattern-matching sense) to the language, so it would be fairly complicated. A simple option would be to just support lists and objects: function () {
with (match("[a, b]")) { return a + b }
with (match("[a]")) { return a }
with (match("[]")) { return 0 }
}
You could imagine this syntax being streamlined a bit.Basically, the with statement gives you a bit of access to the variable scope which is very useful in adding features to the language. It isn't quite as flexible as some alternatives (e.g. macros) but it can still be useful.
As mentioned elsewhere, using `with` is very slow: http://jsperf.com/with-versus-without