17

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

4
  • 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? Commented May 12, 2011 at 15:54
  • 1
    is different because it's evaluated at compile time, not at runtime. Commented May 12, 2011 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 Commented May 12, 2011 at 15:59
  • possible duplicate of How can I simulate macros in JavaScript? Commented Sep 28, 2014 at 3:54

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

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.

Comments

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

Comments

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/

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.