4 ms·
Does the function argument (i.e. the template string) get evaluated regardless of the optional chaining or does it match up with the “roughly equivalent” code?
by koolba 7y ago
Does the function argument (i.e. the template string) get evaluated regardless of the optional chaining or does it match up with the “roughly equivalent” code?
log?.(`Request started at ${new Date().toISOString()}`);
// roughly equivalent to
// if (log != null) {
// log(`Request started at ${new Date().toISOString()}`);
// }
- deleted 7y ago[deleted]
- sceutre 7y agoThe latter. You can look at what javascript is emitted on the playground. let bar: any, log: any; log?.(`foo ${bar()}`); // becomes var _a; var bar, log; (_a = log) === null || _a === void 0 ? void 0 : _a("foo " + bar());
- sicromoft 7y agoWhy would it not match up with the example given? They provided it so you'd know the answer to your question.
- koolba 7y agoI copied the example from the article but it says “roughly” so it’s not entirely clear. The situation that popped in my mind was something like: f?(++a) Normally you’d expect the side effects incrementation to occur prior to the start of the function invocation. If the function is not evaluated it’s not clear from the article if increment will occur.
- plexicle 7y ago`?` will short-circuit. Nothing is evaluated after that.