javascript - Javascript 中的奇怪子句 the Good Parts

标签 javascript clause

此代码来自 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 === valuevalue 不是字符串。

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/

相关文章:

JavaScript 正则表达式 : Tags

javascript - 访问谷歌分析管理 API

mysql - 在 MySQL 的 LIMIT 子句中使用变量

mysql - 为什么mysql的where子句中不能使用变量

sql - 为什么 SQL 对子句顺序严格?

javascript - 单击按钮即可重新启动我的 Canvas

javascript - 为什么 .clearRect 函数无法正常工作?

javascript - 数组的 Angular Directive(指令)

c# - LINQ where子句疑惑

ruby-on-rails - 如何在 rails 中使用 like 子句查询?