是否有可能有条件地应用字符串修饰符方法,如.toLowerCase()
,以一种更聪明的方法:
const text = 'ThIs Is SoMe TeXt';
const maybeConvert = (text, toLower = false) => {
return toLower ? text.toLowerCase() : text;
}
如果你只希望在condition为真时执行操作而不希望在condition为假时执行操作
const text = 'ThIs Is SoMe TeXt';
const maybeConvert = (text, condition) => {
return condition && text.toLowerCase();
}