In javascript, if a function creates a function and then returns it, the returned function's scope is relative to where it was created.
I'm wondering if it's possible to have a block of code stored in a variable which acts almost like preprocessor macros in C/C++, but with the difference being it can be treated like a variable? Where, essentially, you create this macro, assign it to a variable, and wherever the macro is executed, the scope of the code is always relative to where it was executed.
An example (not real code):
macro someMacro {
this.i++
a++
}
function someFunc() {
this.i = 0;
var a = 0;
console.log(this.i, a) // 0, 0
someMacro();
console.log(this.i, a) // 1, 1
}
I saw this question, but that's not quite what I want.
Thanks.
.bind()developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…athere is imho no way except doing some dirtyevalstuff. But I think the question is why you would like to do that. If someone reads your code no one would expect thatsomeMacro()could/would changea, so this would be bad practice anyway.sweetjswhich provides macros as a transcompiler.