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__
#define MYDEBUG(##x) debug(__FILE__,x)
#else
#define debug
#endif
Not necessarily similar, but I want to acheieve that functionality. Is there a way I can do that?
Thanks
@NicolasBousquet Macros give you the ability to manipulate code before it gets executed (and before it gets compiled, macro expansion time). You cannot do that without macros. In the end, the code generated by macros is as powerful as the code written by hand, but any turing-complete language would give you that.
A macro (which stands for "macroinstruction") is a programmable pattern which translates a certain sequence of input into a preset sequence of output. Macros can make tasks less repetitive by representing a complicated sequence of keystrokes, mouse movements, commands, or other types of input.
What Does Macro Mean? A macro is an automated input sequence that imitates keystrokes or mouse actions. A macro is typically used to replace a repetitive series of keyboard and mouse actions and used often in spreadsheets and word processing applications like MS Excel and MS Word.
To use a macro as a JavaScript string, simply insert the macro name between double or single quotes. For example: var username = "%USERNAME%"; When using a macro as a JavaScript string, be aware that the macro value may contain URI encoding or HTML entity encoding.
var de = false; // true when debugging
function bug( msg ){ ... display msg ... }
Usage:
de&&bug( "hello world")
When "de" is false (production mode), the overhead is minimal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With