这个表达式在 JavaScript 中意味着什么?
$('.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它利用短路评估。虽然左侧是真实,但它不会费心评估右侧。
这是因为使用
OR
时,如果一个条件是真实,则不需要考虑其他条件,因为它已经知道足够的信息来回答该条件。它们也是从左到右评估的。此外,在 JavaScript 中,它返回最后计算的操作数,而不是返回 true 或 false 的条件。您会明白为什么这很有用。
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.
||是一个逻辑运算符
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.
它是逻辑 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.该表达式似乎是淡入。
逻辑 OR
||
类似于if
语句的else
情况。如果左侧的计算结果为 false,则创建该函数并调用警报。给定:
在这种情况下,函数声明将被赋值:
请注意,“complete”属性不会保存值,而只是一个函数定义,您需要像
complete()
那样调用complete。对于要在左侧为false
时调用的函数,您必须将其括在括号中并调用它:注意:只有当某些内容未定义(我认为),或者如果
i
结果是时,左侧才会为 false等于行*列-1
。The expression seems to be a fade in.
The logical OR
||
is similar to anelse
case of anif
statement. If the left side evaluates to false, the function is created and an alert is called.Given:
The function declaration would be assigned in this case:
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 isfalse
, you'd have to surround it in parentheses and call it:Note: The left hand side will only be false if something is undefined (i think), or if
i
turns out to equalrows*cols -1
.