2014-01-17 3 views
1

이 코드는 recursion의 섹션에서 가져온 것입니다.Javascript의 이상한 절 좋은 부분

typeof actual === 'string' && (actual === value || typeof value !== 'string') 

방법이 다릅니다 :

var getElementsByAttribute = function (att, value) { 
    var results = []; 

    walk_the_DOM(document.body, function (node) { 
     var actual = node.nodeType === 1 && node.getAttribute(att); 
     if (typeof actual === 'string' && 
       (actual === value || typeof value !== 'string')) { 
      results.push(node); 
     } 
    }); 

    return results; 
}; 

나는 아래 조항의 점을 이해하지? 그리고 경우에만 actual 문자열이며, 하나 actual === value, 또는 value이 문자열이 아닌 경우

typeof actual === 'string' && actual === value 
+3

첫 번째 조건은 '값 === 42'에 대해 true입니다. – Tibos

+0

오오오, 이처럼 5 분 동안 쳐다 보면서 마침내 두 개의 다른 변수의 '문자열'이 '값'과 '실제' . –

+0

@AlexWayne 나는 더 오랫동안 응시해야했다 ... –

답변

2
typeof actual === 'string' && (actual === value || typeof value !== 'string') 

true를 반환합니다. 그리고 경우에만 actual 문자열 및 actual === value 중 하나 인 경우

typeof actual === 'string' && actual === value 

true를 반환합니다. value 문자열 이외의 경우는 문자열 및 actual에 엄격하게 동일한 경우 두 번째 조건은 true를 반환하는 반면 즉

는 첫 번째 조건은, true를 돌려줍니다.