JavaScript。这个表达式的意思是什么?”

时间:2021-07-24 14:47:59
    // 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;
    };

I don't understand what this line of code mean:

我不明白这一行代码是什么意思:

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

How to explain this kind of expression?

如何解释这种表达方式?

a = b === xxx && yyy

Thanks.

谢谢。

1 个解决方案

#1


7  

This is a combination of short-circuiting and the fact that JavaScript returns the final value of a statement. It's the same as:

这是短路和JavaScript返回语句的最终值的组合。这是一样的:

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

Read more here: http://en.wikipedia.org/wiki/Short-circuit_evaluation and https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators#Short-Circuit_Evaluation

在这里阅读更多:http://en.wikipedia.org/wiki/short - 3环评估和https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators# short - 3环评估

#1


7  

This is a combination of short-circuiting and the fact that JavaScript returns the final value of a statement. It's the same as:

这是短路和JavaScript返回语句的最终值的组合。这是一样的:

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

Read more here: http://en.wikipedia.org/wiki/Short-circuit_evaluation and https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators#Short-Circuit_Evaluation

在这里阅读更多:http://en.wikipedia.org/wiki/short - 3环评估和https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators# short - 3环评估