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

Define a walk_the_DOM function that visits every node of the tree in HTML source order, starting

    // 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.

    // 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.

Share Improve this question edited Jun 5, 2012 at 11:29 Fabrizio Calderan 123k26 gold badges170 silver badges182 bronze badges asked Jun 5, 2012 at 11:26 gilzerogilzero 1,9124 gold badges20 silver badges28 bronze badges 1
  • developer.mozilla/en/JavaScript/Reference/Operators/… – Quentin Commented Jun 5, 2012 at 11:28
Add a ment  | 

1 Answer 1

Reset to default 9

This is a bination 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/wiki/Short-circuit_evaluation and https://developer.mozilla/en/JavaScript/Reference/Operators/Logical_Operators#Short-Circuit_Evaluation

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744381611a4571458.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信