Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macro definition in javascript

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

like image 841
Kiran Avatar asked May 12 '11 15:05

Kiran


People also ask

What are macros in JavaScript?

@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.

What is macro define with example?

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.

How macros define?

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.

How do you code a macro in JavaScript?

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.


1 Answers

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.

like image 169
JeanHuguesRobert Avatar answered Sep 17 '22 18:09

JeanHuguesRobert