简体   繁体   English

短路评估不确定吗?

[英]Short Circuit Evaluation to undefined?

I am using short circuit evaluation to set a key in an object. 我正在使用短路评估来设置对象中的键。 For example: 例如:

var obj = {foo : 'bar'}
var obj2 = {}

obj2.somekey = obj.baz || obj.foo //will assign 'bar' to obj2.somekey

The only difference with my code from that above, is that instead of assigning obj.foo if obj.baz doesn't exist, I would like obj.somekey to stay undefined. 与上面代码的唯一区别是,如果obj.baz不存在,则不分配obj.foo,而是希望obj.somekey保持未定义状态。 I tried obj2.somekey = obj.baz || 我尝试了obj2.somekey = obj.baz || , but it threw me a syntax error. ,但这使我陷入语法错误。

Note that null and undefined , as this question suggests, are not actually undefined either. 请注意,正如这个问题所暗示的, nullundefined实际上也不是未定义的。

So is there any way I can do this with short circuit syntax? 那么,有什么办法可以用短路语法做到这一点?

It seems like the simplest solution would be: 似乎最简单的解决方案是:

obj2.somekey = obj.baz;

But if you really want the semantics of the logical operator (ie empty string, null , 0 , NaN , and false , all evaluate to false and so || returns whatever is on the right), use void : 但是,如果您确实想要逻辑运算符的语义(即,空字符串, null0NaNfalse ,所有结果都为false ,那么||返回右边的内容),请使用void

obj2.somekey = obj.baz || void(0);

Note that in this case the property .somekey will be assigned, but the value assigned to it may be undefined . 请注意,在这种情况下,将分配属性.somekey ,但是分配给它的值可能是undefined See Felix Kling 's excellent answer for a full explanation. 有关完整说明,请参见Felix Kling的出色答案 To leave the property .somekey itself undefined simply use: 要保留属性.somekey本身未定义,只需使用:

if ('baz' in obj) obj2.somekey = obj.baz;

Or to leave .somekey undefined even if .baz is falsey, use: 或即使.baz为false,也要使.somekey未定义状态,请使用:

if (obj.baz) obj2.somekey = obj.baz;

I would like obj.somekey to stay undefined. 我希望obj.somekey保持未定义状态。

Just to be clear: There is a difference between a property being undefined (with which I mean "it doesn't exist") and a property having the value undefined . 只是要清楚一点:未定义的属性(我的意思是“它不存在”)与值为undefined的属性之间有区别。 No matter what you do, if you write 不管你做什么,如果你写

obj.somekey = ...;

the property will be created and will turn up in for...in loops for example. 例如,该属性将被创建并将出现在for...in循环中。

If you really want to create the property only when the other property exists, you have to use an if statement: 如果您确实只想在另一个属性存在时才创建该属性,则必须使用if语句:

if (obj.baz) {
   obj.somekey = obj.baz;
}

or the slightly less readable form* * *: 或可读性较差的形式* * *:

obj.baz && (obj.somekey = obj.baz)

Because of short circuiting, the right hand operand will only be evaluated if the left hand operand evaluates to true. 由于短路,仅当左侧操作数的值为true时,才会对右侧操作数的值进行评估。


* Personally I advocate against this style, because it uses the AND operator for side effects, and doesn't do anything with the return value. *我个人反对这种风格,因为它使用AND运算符产生副作用,并且对返回值不做任何事情。 The situations where you have to use this style, because you are forced to use an expression, are probably pretty rare. 在那里必须使用这种风格,因为你不得不使用表达式的情况,很可能是相当罕见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM