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?
習慣沉默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'
代言2017-07-05 11:09:11
If el exists, the following function will be called, otherwise it will not be called