15

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

Kiran
  • 5,478
  • 13
  • 56
  • 84
  • In what way would using something akin to a macro here be different than just using inline programming logic you store in a function and call at will? – Gordon Seidoh Worley May 12 '11 at 15:54
  • 1
    is different because it's evaluated at compile time, not at runtime. – stivlo May 12 '11 at 15:58
  • Macro automatically picks up the __FILE__, __FUNCTION__ (if compiler allows) and __LINE__ automatically.. this makes the code look cleaner as we are not cluttering it with this information with every function call – Kiran May 12 '11 at 15:59
  • possible duplicate of [How can I simulate macros in JavaScript?](http://stackoverflow.com/questions/193536/how-can-i-simulate-macros-in-javascript) – Anderson Green Sep 28 '14 at 03:54

6 Answers6

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

Tushar
  • 85,780
  • 21
  • 159
  • 179
JeanHuguesRobert
  • 696
  • 5
  • 10
  • 1
    This is very clever. The problem with programmatic solutions is that if the debug function is argument-heavy, you have to pay for computing and passing the arguments to the function body, even if the function is empty. This trick avoids that problem -- in production code the actual debug function is never called. – bearvarine Aug 10 '16 at 02:13
1

For those who are still interested:

https://github.com/dcodeIO/Preprocessor.js

A JavaScript source file preprocessor in pure JavaScript, e.g. to build different versions of a library.

Examples:

// #ifdef FULL
console.log("Including extension");
// #include "path/to/extension.js"
// #else
console.log("Not including extension");
// #endif

// #if 1==2
console.log("1==2");
// #elif 2==2
console.log("2==2");
// #endif
aMarCruz
  • 2,434
  • 1
  • 16
  • 14
1

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() {
   <!-- document.write( "blah" ); -->
};

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.

Olly
  • 21
  • 7
1

Javascript has no macros since there is no compiler. You could use console.log and write a regex to strip those statements when deploying.

sapht
  • 2,789
  • 18
  • 16
0

With Builderhttps://github.com/electricimp/Builder, you can do like:

@macro MYDEBUG(x)
  debug(@{__FILE__}, @{x});
@end

...

@{MYDEBUG(100500)}

Also supports for includes directly from GitHub.

mym
  • 252
  • 2
  • 9
0

While is true that there is no compile time as @sapht said, you can pre-process your files if you want. Typically I use an ant script to combine many Javascript files together and add build information.

From a google search I see there is a Javascript preprocessor that you may find interesting: http://www.bramstein.com/projects/preprocess/

stivlo
  • 83,644
  • 31
  • 142
  • 199