Question

This code is from the section of recursion.

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 the point of the clause below:

typeof actual === 'string' && (actual === value || typeof value !== 'string')

How is it different from?

typeof actual === 'string' && actual === value
Était-ce utile?

La solution

typeof actual === 'string' && (actual === value || typeof value !== 'string')

This will return true if and only if actual is a string, and either actual === value, or value is not a string.

typeof actual === 'string' && actual === value

This will return true if and only if actual is a string and either actual === value.

In other words, the first condition returns true if value is anything other than a string, whereas the second condition will only return true if it is a string, and strictly equal to actual.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top