How can I simulate macros in JavaScript?

后端 未结 8 1147
悲哀的现实
悲哀的现实 2020-12-05 10:02

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

相关标签:
8条回答
  • 2020-12-05 10:45
    function unless(condition,body) {
        return 'if(! '+condition.toSource()+'() ) {' + body.toSource()+'(); }';
    }
    
    
    eval(unless( function() {
        return false;
      }, function() {
        alert("OK");
    }));
    
    0 讨论(0)
  • 2020-12-05 10:47

    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.

    0 讨论(0)
  • 2020-12-05 10:47

    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.

    0 讨论(0)
  • 2020-12-05 10:49

    You could use parenscript. That'll give you macros for Javascript.

    0 讨论(0)
  • 2020-12-05 10:57

    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.

    0 讨论(0)
  • 2020-12-05 10:58

    Javascript is interpreted. Eval isn't any more costly that anything else in Javascript.

    0 讨论(0)
提交回复
热议问题