javascript (was Re: Java > Scala -> new thread: GUI for D)

Adam D. Ruppe destructionator at gmail.com
Sun Dec 4 12:17:34 PST 2011


Jacob Carlborg Wrote:
> I hide JavaScript behind CoffeeScript, makes it a bit more usable.

If you like the idea there, but want something a lot more conservative,
in my html.d (in here: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff )
there's now a JavascriptMacroExpander class which pre-processes javascript.

The only build in function it provides right now is foreach() (just because I find
it's lack to be the easiest thing to take care of...) but the macro system might
be useful too for certain things.

Again, it's just a preprocessor, so you drop stuff like this into the middle of
regular js code:

¤foreach(element; document.querySelectorAll("p")) {
     element.style.color = "red";
}

converts to

    var arsd_foreach_loop_temporary_2 = document.querySelectorAll("p");
    for(var arsd_foreach_loop_counter_1 = 0; arsd_foreach_loop_counter_1 < arsd_foreach_loop_temporary_2.length; arsd_foreach_loop_counter_1++) {
             var element = arsd_foreach_loop_temporary_2[arsd_foreach_loop_counter_1];
             element.style.color = "red";
    }


which I realize is an ugly mess but just because I used long names for the temporaries.
It: a) puts the iterable in it's own var, b) does a for loop over it, c) sets up a local for
the iterated thing inside the loop and d) pastes your code in there.        


(that symbol before foreach is the one I did to denote a macro. I put it
on a hotkey in my editor... but I wanted something that I'd never use in
normal code, to keep the preprocessor both dead simple and out of the
way.)


You can also define your own macros and variables in the JS code
or as delegates in D.


I actually wrote this macro system for css, but it works pretty well
here too. (html.d also includes a CssMacroExpander, which runs this
and a de-nesting function.)


This is pretty new stuff, so I'll probably be adding more functions
as I use it more.


More information about the Digitalmars-d mailing list