与&&一起返回

2022-01-19 00:00:00 return operators javascript

用&&返回值是什么意思?

What does it mean to return a value with &&?

else if (document.defaultView && document.defaultView.getComputedStyle) {

    // It uses the traditional ' text-align' style of rule writing, 
    // instead of textAlign
    name = name.replace(/([A-Z]) /g, " -$1" );
    name = name.toLowerCase();
    // Get the style object and get the value of the property (if it exists)
    var s = document.defaultView.getComputedStyle(elem, " ") ;
    return s && s.getPropertyValue(name) ;

推荐答案

return a &&b 意思是如果 a 为假则返回 a,如果 a 为真则返回 b".

return a && b means "return a if a is falsy, return b if a is truthy".

相当于

if (a) return b;
else return a;

相关文章