最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论
    // 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

发布评论

评论列表(0)

  1. 暂无评论