Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript. What does this expression mean: " var a = b === c && d; "

Tags:

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;
    };

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.

like image 504
gilzero Avatar asked Jan 16 '23 12:01

gilzero


1 Answers

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

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

like image 138
Matt Avatar answered Jan 31 '23 07:01

Matt