Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Future of the with-statement in Javascript

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?

like image 439
Epeli Avatar asked Mar 21 '11 03:03

Epeli


1 Answers

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");
     });
   }
 */
like image 89
Adrien Avatar answered Oct 29 '22 14:10

Adrien