I know that usage of the with-statement is not recommended in Javascript and is forbidden in ECMAScript 5, but it allows one to create some nice DSLs in Javascript.
For example CoffeeKup-templating engine and the Zappa web DSL. Those uses some very weird scoping methods with the with-statement to achieve DSLish feeling to them.
Is there any future with the with-statement and these kinds of DSLs?
Can this DSL-effect be achieved without the with-statement?
I know that usage of the with-statement is not recommended in Javascript and is forbidden in ECMAScript 5, but it allows one to create some nice DSLs in Javascript.
For example CoffeeKup-templating engine and the Zappa web DSL. Those uses some very weird scoping methods with the with-statement to achieve DSLish feeling to them.
Is there any future with the with-statement and these kinds of DSLs?
Can this DSL-effect be achieved without the with-statement?
Share Improve this question edited Mar 21, 2011 at 3:59 esamatti asked Mar 21, 2011 at 3:37 esamattiesamatti 19k11 gold badges81 silver badges83 bronze badges 1- ES Next quasi-literals look like they will become JS DSL tool. – c69 Commented Oct 4, 2011 at 17:27
4 Answers
Reset to default 10In coffeescript, there is a nice trick to keep using fancy dsls without using with
:
using = (ob, fn) -> fn.apply(ob)
html =
head : (obj) -> # implementation
body : (fn) -> # implementation
div : (str) -> # implementation
using html, ->
@head
title: "My title"
@body =>
@div "foo bar"
@div "qux moo"
/*
in pure javascript you'd be using
with(html){
head({title:"My title"});
body(function(){
div("foo bar");
div("qux moo");
});
}
*/
with
being "forbidden" in ECMAScript 5 is a common misconception.
Only in strict mode of ECMAScript 5 — which is opt-in, mind you — with
statement is a syntax error. So you can certainly still use with
in fully ECMAScript 5 -compliant implementations, as long as they occur in non-strict (or sloppy, as Crockford calls it) code. It won't be pretty for performance (since mere presence of with
often kills various optimizations in modern engines) but it will work.
Future versions of ECMAScript are very likely to be based on strict mode behavior, although will also likely be opt-in as well. So conforming to strict mode is certainly a good idea when it comes to future proofing your scripts.
Why not just assign a var to point to the object instead of using with?
'with' style:
with(a_long_object_name_that_is_bloated) {
propertyA = 'moo';
propertyB = 'cow';
}
'var' style:
var o = a_long_object_name_that_is_bloated;
o.propertyA = 'moo';
o.propertyB = 'cow';
To answer Epeli's question, take a look at CoffeeMugg which does what CoffeeKup does but using Adrien's technique. It uses this.
instead of the with
statement.