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?
In 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");
});
}
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With