operator to call a method of an object. For instance, something like:
obj.[isTrue ? doTrue() : doFalse()]
const operation = isTrue ? methodKey1 : methodKey2; obj[operation]();
One more option, designed especially for this case, is "with". But it's usage is considered a bad practice, it may lead to bugs that are very hard to detect. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with Anyway, your example is a bad style. It's better to use ternary not to just call function, but to evaluate and assign/use result of that call. Example. Good style: temperature = isCelsius ? term.getCelsius() : term.getFahrenheit(); Bad style: shouldDoOne ? doOne() : doAnother();
Обсуждают сегодня