使用 || 在 javascript 中初始化对象 操作员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7118575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:00:46  来源:igfitidea点击:

Init object in javascript using || operator

javascript

提问by Kai

Sometimes I see in javascript code something like this:

有时我会在 javascript 代码中看到这样的内容:

var myObj = myObj || {};

So, what actually happen here? I suppose || operator returns true or false, but it's not correct.

那么,这里究竟发生了什么?我想|| 运算符返回 true 或 false,但它不正确。

回答by cdhowie

The ||operator returns the left operand if it evaluates as true, otherwise it evaluates and returns the right operand. In other words, a || bis equivalent to a ? a : bexcept that ais only evaluated once.

||运营商如果结果为真,否则计算并返回正确的操作数返回左操作数。换句话说,a || b相当于a ? a : b除了a只被评估一次。

回答by Joe.wang

To understand the || operator, let's first look at a fairly basic example. The logical OR operator may be used to provide a default value for a defined variable as follows:

理解 || 运算符,让我们先看一个相当基本的例子。逻辑 OR 运算符可用于为定义的变量提供默认值,如下所示:

 var bar = false,  
 foobar = 5,  
 foo = bar || foobar; // foo = 5  

In this case, foo will only be assigned the value of foobar if bar is considered falsy. A falsy value could be considered being equal to 0, false, undefined, null, NaN or empty (e.g "").

在这种情况下,如果 bar 被认为是假的,则 foo 只会被分配 foobar 的值。假值可以被视为等于 0、假、未定义、空、NaN 或空(例如“”)。

回答by Jan Aagaard

This initializes myObj unless it is already defined.

这将初始化 myObj 除非它已经定义。

回答by Doug Kress

The OR op (||) will return the first non-empty/false parameter.

OR 操作 (||) 将返回第一个非空/假参数。

In the case specified, if myObj is false or null, it will be set to an empty object (the {} brackets are used to create objects)

在指定的情况下,如果 myObj 为 false 或 null,则将其设置为空对象({} 括号用于创建对象)

回答by zatatatata

You can use this construct to get the object that is not null, undefined, etc. This is used in cases where you use myObjlater on in the code which requires it to be an object. If, for some reason, myObjis undefined prior to this line, re-assigning it leaves it undefinedor null, in which case it would be assigned {}.

您可以使用此构造来获取不是nullundefined等的对象。这用于myObj稍后在需要它是对象的代码中使用的情况。如果由于某种原因myObj在此行之前未定义,则重新分配它会留下它undefinednull,在这种情况下,它将被分配{}

You can think of this as:

您可以将其视为:

// If the object is already defined
if (myObj)
    var myObj = myObj;
// It was undefined or null, so assign an empty object to it.
else
    var myObj = {};

回答by Petar Ivanov

|| is a short circuit operator. If the first operand evaluates to true the second is not evaluated.

|| 是短路算子。如果第一个操作数的计算结果为真,则不计算第二个操作数。

Thus JS a || bis something like c# a ?? b

因此 JSa || b类似于 c#a ?? b

回答by Fender

if myObjis undefined or null then it evaluates the expression on the right side of ||which creates a new empty object

如果myObj是 undefined 或 null 则它评估右侧的表达式,该表达式||创建一个新的空对象

so myObjis either myObj if it is not null or an empty object if myObj is null

所以myObj要么MyObj中,如果它不是空值或一个空对象如果MyObj中为空

i hope you understand what i mean

我希望你明白我的意思