Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不应该';";退货单据&&;doc.userId==userId&引用;返回错误?_Javascript_Meteor - Fatal编程技术网

Javascript 不应该';";退货单据&&;doc.userId==userId&引用;返回错误?

Javascript 不应该';";退货单据&&;doc.userId==userId&引用;返回错误?,javascript,meteor,Javascript,Meteor,我将遵循承诺8-1,并在承诺8-1之后添加: // Check that the userId specified owns the documents ownsDocument = function(userId, doc) { return doc && doc.userId === userId; } 我不明白的是doc和userId并不是严格相等的(==),那么为什么这样做呢 供参考: 文件: doc.userId: userId: 0w9x8y7z 用户标识:

我将遵循承诺8-1,并在承诺8-1之后添加:

// Check that the userId specified owns the documents
ownsDocument = function(userId, doc) {
  return doc && doc.userId === userId;
}
我不明白的是doc和userId并不是严格相等的(==),那么为什么这样做呢

供参考:

文件:

doc.userId:

userId: 0w9x8y7z
用户标识:

userId: 0w9x8y7z

我知道
doc.userId===userId
,但是为什么
doc&&doc.userId===userId
会通过呢?

这是JavaScript中常用的习惯用法,如果要在访问对象的某个属性之前检查对象是否
未定义(或其他),可以说:

obj && obj.prop
如果第一个参数为false,则不会计算第二个参数,因此这可以防止代码尝试从值未定义的变量检索属性值

因此,示例中的return语句等价于:

if (doc){
    return doc.userId;
} else {
    return doc;
}

这是JavaScript中常用的习惯用法,如果要在访问对象的某个属性之前检查对象是否未定义(或其他),可以说:

obj && obj.prop
如果第一个参数为false,则不会计算第二个参数,因此这可以防止代码尝试从值未定义的变量检索属性值

因此,示例中的return语句等价于:

if (doc){
    return doc.userId;
} else {
    return doc;
}
如果选中,您将看到相等(
=
/
==
)的优先级高于AND(
&&
),因此:

doc && doc.userId === userId
是这样分析的:

doc && (doc.userId === userId)
而不是这个:

(doc && doc.userId) === userId
然后,由于
doc
是真实的,引擎计算
doc.userId===userId
并返回结果。

如果您检查,您将看到等式(
==
/
===
)的优先级高于and(
&
),因此:

doc && doc.userId === userId
是这样分析的:

doc && (doc.userId === userId)
而不是这个:

(doc && doc.userId) === userId

然后,由于
doc
是真实的,因此引擎计算
doc.userId===userId
并返回该结果。

您困惑的根源似乎是您解释了以下内容:

if(doc && doc.userId === userId)
如“如果doc和doc.userId都等于userId”。事实并非如此

&&
的每一侧都是一个单独的条件。这与:

if(doc == true && doc.userId === userId)
其次,左侧的
==true
部分是冗余的,因此在检查变量是否正确时通常不包括该部分


检查的原因是,在尝试访问
doc
上的属性之前,您希望检查该文档是否不存在。使用逻辑
,如果左侧的计算结果不为true,则不会执行右侧。如果未进行检查,则如果
doc
,则尝试访问none对象上的属性时会产生错误。

您困惑的根源似乎是您解释了以下内容:

if(doc && doc.userId === userId)
如“如果doc和doc.userId都等于userId”。事实并非如此

&&
的每一侧都是一个单独的条件。这与:

if(doc == true && doc.userId === userId)
其次,左侧的
==true
部分是冗余的,因此在检查变量是否正确时通常不包括该部分


检查的原因是,在尝试访问
doc
上的属性之前,您希望检查该文档是否不存在。使用逻辑
,如果左侧的计算结果不为true,则不会执行右侧。如果未进行检查,则如果
doc
为空,则尝试访问none对象上的属性时会产生错误。

doc
本身的计算结果为
true
(它是一个对象;不是0,不是null“”,或者在布尔上下文中计算为
false
)如果正确,则对
&
之后的下一个测试进行评估。(这就像
doc
null
(错误)试图访问属性
doc.userId
会导致错误一样)作为@AlexK。如上所述,它不会求值为false,因此该语句将继续求值。这就是所谓的短路技术。基本上,在尝试访问其属性之前,最好先检查
doc
是否存在。如果
doc
未定义,它将返回false,而不考虑它(未定义)是否具有userId属性,以及该属性是否等于提供的
userId
。一个澄清。如果
doc
undefined
,则将返回
undefined
,而不是
false
。这最好写成
return!!doc&&doc.userId==userId
doc
本身被评估为
true
(它是一个对象;不是0,null“”,或者在布尔上下文中被评估为
false
的任何其他对象)作为它的true,然后评估
和&
之后的下一个测试。(这就像
doc
null
(错误)试图访问属性
doc.userId
会导致错误一样)作为@AlexK。如上所述,它不会求值为false,因此该语句将继续求值。这就是所谓的短路技术。基本上,在尝试访问其属性之前,最好先检查
doc
是否存在。如果
doc
未定义,它将返回false,而不考虑它(未定义)是否具有userId属性,以及该属性是否等于提供的
userId
。一个澄清。如果
doc
undefined
,则将返回
undefined
,而不是
false
。这最好写成
return!!doc&&doc.userId==userId
.ohhhh,所以我检查
doc
是否存在,然后检查
doc.userId=userId
。现在这就更有意义了。非常感谢!Ohhhh,所以我正在检查
doc
是否存在,然后检查
doc.userId=userId
。现在这就更有意义了。非常感谢!