此代码来自 recursion 的部分.
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;
};
我不明白下面从句的要点:
typeof actual === 'string' && (actual === value || typeof value !== 'string')
它与什么不同?
typeof actual === 'string' && actual === value
最佳答案
typeof actual === 'string' && (actual === value || typeof value !== 'string')
这将返回 true
当且仅当 actual
是一个字符串,并且 actual === value
或 value
不是字符串。
typeof actual === 'string' && actual === value
当且仅当 actual
是一个字符串并且 actual === value
时,这将返回 true
。
换句话说,如果 value
不是字符串,则第一个条件返回 true,而第二个条件只有在它是字符串且严格等于 actual 时才返回 true
。
关于javascript - Javascript 中的奇怪子句 the Good Parts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194094/