What does the '&&' expression mean in this snippet of JavaScript? [closed]

醉酒当歌 提交于 2021-01-29 02:50:51

问题


Hi, I recently got a piece of JavaScript code snippet from here.

Could someone please explain how this statement works: (jq=jq.slice(1)).length && hidenext(jq);in the function below?

(function hidenext(jq){
    jq.eq(0).fadeOut("fast", function(){
        (jq=jq.slice(1)).length && hidenext(jq);
    });
})($('div#bodyContent a'))

回答1:


Here, as the result isn't used, the logical AND is used as a kind of short if.

It's the same as

if ((jq=jq.slice(1)).length) hidenext(jq);

It uses the fact that the second part won't be evaluated if the first one doesn't evaluate as true.




回答2:


It means that hidenext(jq) will only be called if jq (now set to the return value of jq.slice(1) has a length > 0. Because && is short-circuited, evaluation is halted if the first operand is falsy.



来源:https://stackoverflow.com/questions/15621527/what-does-the-expression-mean-in-this-snippet-of-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!