search

Home  >  Q&A  >  body text

javascript - What does this el && fn.call(el, e, el) code mean?

el && fn.call(el, e, el) I feel a little confused when this code is written like this.
If you want to call fn, why not write fn.call(el, e, el) directly. However, there is an & sign in front of
. He wants to find a Boolean value without returning. What's the point of finding true or false
in this way?

PHP中文网PHP中文网2709 days ago1102

reply all(4)I'll reply

  • 習慣沉默

    習慣沉默2017-07-05 11:09:11

    Uses the short circuit property of &&.

    in A && B

    means that if A is false, then the entire expression is false and there is no need to evaluate B.
    If A is true, then evaluate B to judge

    So the above code means

    If el is true, then execute fn.call(el, e, el);


    || also has similar properties:

    If the lvalue is true, the following does not need to be evaluated.

    For example, used to specify the default value

    function test(a){
        a = a || '默认值'; 
        
        return a; 
    }
    
    test(); 
    // => '默认值'
    test('wow'); 
    // => 'wow' 

    reply
    0
  • 代言

    代言2017-07-05 11:09:11

    If el exists, the following function will be called, otherwise it will not be called

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-05 11:09:11

    // 如果el为空会报错,所以加个判断
    if(el){
      fn.call(el, e, el)
    }

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 11:09:11

    a && b is equivalent to
    if (a){

    b

    }

    reply
    0
  • Cancelreply