search

Home  >  Q&A  >  body text

javascript - When doing || operation between jquery objects, why is the returned value not of boolean type?

jquery source code snippets are as follows:

        // HANDLE: $(expr, $(...))
        } else if ( !context || context.jquery ) {
            return ( context || rootjQuery ).find( selector );

I think context here is a jquery object, rootjquery is $(document)

But shouldn’t the demerit they return be a Boolean value? Why is it a jquery object here?

So what does the || operation do here? What is its role???

Thanks

漂亮男人漂亮男人2826 days ago565

reply all(2)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:37:57

    Please see here for the basics, which is clear at a glance.
    https://developer.mozilla.org...

    One more thing, before ES2015, JS functions did not have default parameters.
    In order to use default parameters like other languages, they were often written like this

    function demo(arg){
        arg = arg || true;
    }

    In order to be compatible with different browser APIs, we often write like this.

    if( !window.requestAnimationFrame ){
        window.requestAnimationFrame = (window.webkitRequestAnimationFrame ||
                                        window.mozRequestAnimationFrame ||
                                        window.msRequestAnimationFrame || 
                                        function (callback){
                                          return setTimeout(callback, 1000 / 60);
                                        });
    }

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:37:57

    Javascript logical operators can be used in non-Boolean environments.

    Although the && and || operators can be used in non-Boolean environments, if their return values ​​can be converted to Boolean values, they can also be considered Boolean operations

    (from logical operator)

    For non-Boolean environments:

    When there is one false, return the value on the false side
    When there are two false, return the value before the operator (left side);
    When there are two true, return the value after the operator (right side) value.

    (derived from the return value of logical operations in JavaScript (logical AND &&, logical OR ||, logical NOT!))

    reply
    0
  • Cancelreply