I know that JavaScript doesn\'t support macros (Lisp-style ones) but I was wondering if anyone had a solution to maybe simulate macros? I Googled it, and one of the solution
function unless(condition,body) {
return 'if(! '+condition.toSource()+'() ) {' + body.toSource()+'(); }';
}
eval(unless( function() {
return false;
}, function() {
alert("OK");
}));
A library by Mozilla (called SweetJS) is designed to simulate macros in JavaScript. For example, you can use SweetJS to replace the function
keyword with def
.
LispyScript is the latest language that compiles to Javascript, that supports macros. It has a Lisp like tree syntax, but also maintains the same Javascript semantics. Disclaimer: I am the author of LispyScript.
You could use parenscript. That'll give you macros for Javascript.
Check out the Linux/Unix/GNU M4 processor. It is a generic and powerful macro processor for any language. It is especially oriented towards Algol-style languages of which JavaScript is a member.
Javascript is interpreted. Eval isn't any more costly that anything else in Javascript.