javascript - What does this el && fn.call(el, e, el) code mean?
PHP中文网
PHP中文网 2017-07-05 11:07:11
0
4
1129

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中文网

认证0级讲师

reply all(4)
習慣沉默

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' 
代言

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

滿天的星座
// 如果el为空会报错,所以加个判断
if(el){
  fn.call(el, e, el)
}
过去多啦不再A梦

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

b

}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template