My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Wednesday, March 28, 2007

JavaScript Metaprogramming and MetaDOM

I found really interesting Adam McCrea presentation about JavaScript Metaprogramming but I'm not, absolutely, a Prototype fun :P

I thought about another way to show Metaprogramming power and that's why I wrote a simple MetaDOM object.

What can You do with MetaDOM?
Nothing that You can't do with every JavaScript library, get elements, modify them ... but with this object You should use a sintax like this one:

with(MetaDOM){

While("p").Do(AddPNumber);

For("li").In("ul").Do(AddLiNumber);

If("div").class("some-div").Then(WriteSomeDiv);

If("*").class("paragraph").
Then(WriteSomeDiv).
ElseIf("*").id("paragraph").
Then(WriteSomeParagraphId)
;

With("body").Do(function(body){
body.innerHTML = '

MetaDOM Experiment

' + body.innerHTML;
});

If("*").name("h2").Then(AddH3);
};


Semmes cool? This is the example page, tested only with FireFox (and probably this alpha joke version works only with FireFox).

Do You understand better Metaprogramming and do you like it? :D

2 comments:

-dean said...

I like it.

wioota said...

Nice stuff - the code is very literate. I hope to make some time to experiment with this approach.