How can I simulate macros in JavaScript?

后端 未结 8 1160
悲哀的现实
悲哀的现实 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");
    }));
    

提交回复
热议问题