In C++ we can write a preprocessor macro like this:
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
int a=1;
DEBUG(a) //prints "a:1"
I tried to do a similar thing in javascript
var DEBUG= function(name){
return function() {
console.log(name+": ",eval(name))
}
}
{
let a=1;
DEBUG('a')() //a is undefined
}
I was wondering if there was any way to "inline" functions or otherwise do some clever thing with closures which allows DEBUG to be evaluated in the calling scope. I know I could just call DEBUG('a',a) but that wouldn't be as fun ;)
but that wouldn't be as fun- but it's the only wayconsole.log({ a })eval(debug('a')), whereevalis called in the scope where the variable is available. Or you just do the same thing as in C++ and use an actual preprocessor which does macro expansion