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

Jacob Carlborg doob at me.com
Mon Dec 5 04:29:38 PST 2011


On 2011-12-05 10:10, Marco Leise wrote:
> Am 04.12.2011, 21:17 Uhr, schrieb Adam D. Ruppe
> <destructionator at gmail.com>:
>
>> Jacob Carlborg Wrote:
>> 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";
>> }
>
> There is also this macro in the JavaScript plugin for Eclipse:
>
> for (var ${index} = 0; ${index} < ${array}.length; ${index}++) {
> var ${array_element} = ${array}[${index}];
> ${cursor}
> }
>
> This is really one of the largest shortcomings of the language that can
> not be explained with a simple design choice.

CoffeeScript makes it a correct for-each loop:

for e in arr
     # do something with the element "e"

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list