Can && operator use between two object ? And what result will we get?

时间:2019-04-08 13:11:44

标签: javascript

function getParent($this) {
    var selector = $this.attr('data-target') //The jQuery attr() method is also used to set/change attribute values.

    //for ie7
    if (!selector) {
      selector = $this.attr('href')
      selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
    }

    var $parent = selector && $(selector)

    return $parent && $parent.length ? $parent : $this.parent()
}

2 个答案:

答案 0 :(得分:0)

If the Left-Hand Side of a && operator is truthy, then the expression will evaluate as the Right-Hand Side of the operator.

(If the LHS is falsy, it will evaluate as the LHS).

Since all objects are truthy values, the scenario you describe will evaluate as the RHS. Thus:

(someObject && someOtherObject) === someOtherObject

答案 1 :(得分:0)

&& operator is will check if the LHS value is true, else check the RHS is truthy.

If you use that in an assignment operator, the functionality is still the same. It will see if the LHS and RHS is truthy (it should not be null, undefined, false, NaN, Infinity, '' or 0) and assign the RHS to the variable. If not, it will assign the first falsy value.

'a' && 'b'  => 'b'
3 && false => false
null && 'a' => null
'' && true => ''
true && 3 => 3
(() => true)() && 9