Comment on Quote-unquote "macros"parentComments−svieira2yThe key thing to note here is that it is completely _lexical_ in scope - this only generates one tagged template even when called multiple times: function lexicalMagic() { const callHistory = []; function tag(strings, ...values) { callHistory.push(strings); // Return a freshly made object return {}; } function evaluateLiteral() { return tag`Hello, ${"world"}!`; } return { evaluateLiteral, callHistory } } We can create multiple "instances" of our closures, just as we expect and they do in fact produce distinct everything-else: > const first = lexicalMagic() > const second = lexicalMagic() > first.evaluateLiteral === second.evaluateLiteral false > first.callHistory === second.callHistory false Using them doesn't behave any differently than the unclosured versions (as expected): > first.evaluateLiteral() === first.evaluateLiteral() false > first.callHistory[0] === first.callHistory[1] true > second.evaluateLiteral() === second.evaluateLiteral() false > second.callHistory[0] === second.callHistory[1] true However, there is only one instance of the template literal, not two as we might expect. This is really helpful for some cases, but it definitely is surprising given the rest-of-JS: > first.callHistory[0] === second.callHistory[1] true
Comments
The key thing to note here is that it is completely _lexical_ in scope - this only generates one tagged template even when called multiple times:
We can create multiple "instances" of our closures, just as we expect and they do in fact produce distinct everything-else: Using them doesn't behave any differently than the unclosured versions (as expected): However, there is only one instance of the template literal, not two as we might expect. This is really helpful for some cases, but it definitely is surprising given the rest-of-JS: