问题描述
&&"的这些各种用法之间有什么区别?逻辑运算符?
What is the difference between these various uses of the "&&" logical operator?
来自Oliver Steele的Functional.js库.第4行,"args.length&& arg":
From Oliver Steele's Functional.js library.Line 4, "args.length && arg":
0 Function.prototype.partial = function(){
1 var fn = this, args = Array.prototype.slice.call(arguments);
2 return function(){
3 var arg = 0;
4 for ( var i = 0; i < args.length && arg < arguments.length; i++ )
5 if ( args[i] === undefined )
6 args[i] = arguments[arg++];
7 return fn.apply(this, args);
8 };
9 };
来自 bootstrap.js .第11行下面的'hover'&&this.$ element":
From bootstrap.js.Line 11 below, "'hover' && this.$element":
1 var Carousel = function (element, options) {
2 this.$element = $(element).on('keydown.bs.carousel', $.proxy(this.keydown, this))
3 this.$indicators = this.$element.find('.carousel-indicators')
4 this.options = options
5 this.paused =
6 this.sliding =
7 this.interval =
8 this.$active =
9 this.$items = null
10
11 this.options.pause == 'hover' && this.$element
12 .on('mouseenter.bs.carousel', $.proxy(this.pause, this))
13 .on('mouseleave.bs.carousel', $.proxy(this.cycle, this))
14 }
为什么不只在第一个示例中使用+算术运算符?
Also why not just use the + arithmetic operator in the first example?
这是我在bootstrap.js的相同轮播"部分中遇到问题时遇到的另一个例子:
Here is yet another example that I'm having trouble grokking, from the same Carousel section of bootstrap.js:
this.options.interval
&& !this.paused
&& (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
推荐答案
好吧,它到处都是相同的运算符,但是程序员将其用于不同的目的.
Well, it's the same operator everywhere, but being used for different purposes by the programmer.
第一个代码示例根本不执行args.length && arg
,而是执行了(i < args.length) && (arg < arguments.length)
的等效功能,因为&&
的优先级低于<
.这意味着,循环直到i
等于或超过自变量计数或arg
达到或超过自变量计数为止".您可以看到+
因此在这种情况下不是等效的.
The first code example is not doing args.length && arg
at all, but is rather doing the equivalent of (i < args.length) && (arg < arguments.length)
because &&
has lower precedence than <
. It means, "loop until either i
is at or surpasses the argument count or arg
is at or surpasses the argument count". You can see that +
is therefore not equivalent in this context.
其他代码示例都利用以下事实:当逻辑和运算符(&&
)的左操作数为false时,就会短路",即仅在以下情况下才对右侧的表达式求值:
The other code examples are all exploiting the fact that the logical-and operator (&&
) "short-circuits" when its left-hand operand is false, i.e. it only evaluates the expression on the right if the one on the left is true.
因此,this.options.pause == 'hover' && this.$element.on(...
是仅在将suspend选项设置为"hover"时附加事件侦听器的简便方法(因为如果未设置,则左侧表达式将为false,而on
函数将不会被调用.)
So, this.options.pause == 'hover' && this.$element.on(...
is a short-hand way of attaching the event listeners only if the pause option is set to 'hover' (because if it's not, the left-hand expression will be false, and the on
functions won't be called).
类似地,如果所需的时间间隔为"true"(我想这里通常非零,但也未定义,等等),您发现的最后一个示例仅调用setInterval
(并将结果分配给this.interval
).并且轮播没有暂停.
Similarly, the final example you found only calls setInterval
(and assigns the result to this.interval
) if the desired interval is "true" (I imagine generally non-zero here, but also not undefined, etc.) and the carousel is not paused.
这篇关于&&的这两种用法之间有什么区别?逻辑运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!