macro definition in javascript

后端 未结 6 710
一向
一向 2021-01-01 22:07

Is there a way that I can define a macro similar to C/C++ macros in Javascript?

I want to use this for debug statements: Something like

#ifdef TEST         


        
6条回答
  •  离开以前
    2021-01-01 23:11

    There isn't a way to do this in JavaScript. You could have a global variable like

    var isDebugging = false;
    

    Then when writing code, just check if the variable is true. Obviously this will create some unwanted overhead with file size, and a very slight performance loss. But other than specifying your own format, and running the code though a tool to strip debugging code out before you upload.

    Something like

    var foo = function() {
       
    };
    

    For a release build, you would remove everything inside the tags, inclusive. And for a debug build you could just remove the tags, but keep the code. Something like this could be performed with an Ant build script or similar.

提交回复
热议问题