JavaScript的。这个表达的含义是什么:“var a = b === c&& d;”

时间:2012-06-05 11:26:15

标签: javascript

    // Define a walk_the_DOM function that visits every
    // node of the tree in HTML source order, starting
    // from some given node. It invokes a function,
    // passing it each node in turn. walk_the_DOM calls
    // itself to process each of the child nodes.
    var walk_the_DOM = function walk(node, func) {
        func(node);
        node = node.firstChild;
        while (node) {
            walk(node, func);
            node = node.nextSibling;
        }
    };

    // Define a getElementsByAttribute function. It
    // takes an attribute name string and an optional
    // matching value. It calls walk_the_DOM, passing it a
    // function that looks for an attribute name in the
    // node. The matching nodes are accumulated in a
    // results array.
    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;
    };

我不明白这行代码意味着什么:

var actual = node.nodeType === 1 && node.getAttribute(att);

如何解释这种表达方式?

a = b === xxx && yyy

感谢。

1 个答案:

答案 0 :(得分:9)

这是短路和JavaScript返回语句最终值这一事实的组合。它与:

相同
if (b === xxx) {
  a = yyy;
} else {
  a = false;
}

在此处阅读更多内容:http://en.wikipedia.org/wiki/Short-circuit_evaluationhttps://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators#Short-Circuit_Evaluation