这个表达式在 JavaScript 中意味着什么?

发布于 2024-11-16 16:34:08 字数 420 浏览 3 评论 0原文

$('.blocks','#grid').each(function (i) {
              $(this).stop().css({opacity:0}).delay(100).animate({
                'opacity': 1
              }, {
                duration: 2000,
                complete: (i !== row * cols - 1) ||
                function () {
                  alert('done');
                }

              });
            });

“||”是什么意思动画函数的“完整”属性中的运算符意味着什么?

$('.blocks','#grid').each(function (i) {
              $(this).stop().css({opacity:0}).delay(100).animate({
                'opacity': 1
              }, {
                duration: 2000,
                complete: (i !== row * cols - 1) ||
                function () {
                  alert('done');
                }

              });
            });

What does the "||" operator mean in the "complete" property of the animate function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随心而道 2024-11-23 16:34:08

它利用短路评估。虽然左侧是真实,但它不会费心评估右侧。

这是因为使用 OR 时,如果一个条件是真实,则不需要考虑其他条件,因为它已经知道足够的信息来回答该条件。它们也是从左到右评估的。

此外,在 JavaScript 中,它返回最后计算的操作数,而不是返回 truefalse 的条件。您会明白为什么这很有用。

It exploits short circuit evaluation. While the left hand side is truthy it won't bother evaluating the right hand side.

This is because with an OR, if one condition is truthy, it doesn't need to bother with additional conditions because it already knows enough information to answer the condition. They are also evaluated from left to right.

Also, in JavaScript, instead of the condition returning true or false, it returns the last operand it evaluated. You can see why this is useful.

紧拥背影 2024-11-23 16:34:08

||是一个逻辑运算符
http://www.w3schools.com/js/js_comparisons.asp

它将“有利” ' 左侧直到不再满足左侧条件,此时它将执行右侧,这是一个执行警报的匿名函数。

|| is a logical operator
http://www.w3schools.com/js/js_comparisons.asp

it will 'favor' the left hand side until the left hand condition is not met anymore, at which point it will execute the right side, which is an anonymous function that executes an alert.

耳钉梦 2024-11-23 16:34:08

它是逻辑 OR 运算符,但仅在左侧表达式为 false 时才计算右侧表达式。

如果左侧表达式为 true,则它知道整个表达式的计算结果为 true,因此它不会费心计算(或执行)右侧表达式。

It's the logical OR operator, but it only evaluates the right-hand expression if the left-hand expression is false.

If the left-hand expression is true then it knows that the whole expression evaluates to true, so it doesn't bother evaluating (or executing) the right-hand expression.

浪漫之都 2024-11-23 16:34:08

该表达式似乎是淡入。

逻辑 OR || 类似于 if 语句的 else 情况。如果左侧的计算结果为 false,则创建该函数并调用警报。


给定:

  • (i !== row * cols - 1) ||函数(){警报('完成'); }
    

在这种情况下,函数声明将被赋值:

  • false ||函数(){警报('完成'); }
    

请注意,“complete”属性不会保存值,而只是一个函数定义,您需要像 complete() 那样调用complete。对于要在左侧为 false 时调用的函数,您必须将其括在括号中并调用它:

  • complete: (i !== row * cols - 1) || (函数(){警报('完成');})()
    

注意:只有当某些内容未定义(我认为),或者如果 i 结果是时,左侧才会为 false等于行*列-1

The expression seems to be a fade in.

The logical OR || is similar to an else case of an if statement. If the left side evaluates to false, the function is created and an alert is called.


Given:

  • (i !== row * cols - 1) || function () { alert('done');  }
    

The function declaration would be assigned in this case:

  • false || function (){ alert('done'); }
    

Note, that "complete" property would not hold a value, just a function definition, you would need to call complete as something like complete(). For the function to be called if the left side is false, you'd have to surround it in parentheses and call it:

  • complete: (i !== row * cols - 1) || (function () {alert('done');})()
    

Note: The left hand side will only be false if something is undefined (i think), or if i turns out to equal rows*cols -1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文