问题描述
以下是下划线中的一行代码.该行的加前缀是什么?
if(obj.length === + obj.length){//加上前缀?
这将强制 obj.length
的值是 Number
.从本质上讲,这样做是为了确保未重写类似数组的对象的默认 length
值,以便可以正确地对其进行迭代.
breaker
在这种情况下不会做任何事情,因为与 breaker {}
也会计算为 false
./code> ..即使没有等效比较.
但是,在这种情况下不使用 breaker
,因为它是在 .each
函数的外部中定义的,它看起来与您的功能不同在这里显示.相反,它用于迫使其他循环方法中断":
_.every = _.all = function(obj,iterator,context){/*片段*/if(!(结果=结果&& iterator.call(上下文,值,索引,列表)))返回断路器
您可以看到,如果每个"中的结果都不是真实的,我们希望立即中断. _.every
调用 _.each
,它将返回 breaker
,与自身相比,它为true,允许立即中断./p>
Here is a line of code from underscore. What is that plus prefix for in this line?
if (obj.length === +obj.length) { // plus prefix?
This forces the value of obj.length
to be a Number
. Essentially this is done to make sure that the default length
value for an array-like object has not been overridden so that it can be iterated properly.
breaker
will do nothing in this context because even another empty object {}
will evaluate to false
when compared to breaker
.. even without an equivalence comparison.
However, breaker
is not used in that context because it is defined outside of the .each
function, which appears different than what you are showing here. Instead, it is used to force a "break" from other looping methods:
_.every = _.all = function(obj, iterator, context) {
/* snip */
if (!(result = result && iterator.call(context, value, index, list)))
return breaker;
You can see that if the result is not truthy in "every," we want to break immediately. _.every
calls _.each
, and it will return breaker
which will be true when compared to itself, allowing for an immediate break.
这篇关于在这种情况下,+前缀有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!