Ruby 在 JavaScript 中的 ||=(或等于)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19088533/
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-09-06 06:13:06  来源:igfitidea点击:

Ruby's ||= (or equals) in JavaScript?

javascriptrubysyntax

提问by at.

I love Ruby's ||=mechanism. If a variable doesn't exist or is nil, then create it and set it equal to something:

我喜欢 Ruby 的||=机制。如果变量不存在或为nil,则创建它并将其设置为等于某值:

amount # is nil
amount ||= 0 # is 0
amount ||= 5 # is 0

I need to do something similar in JavaScript now. What's the convention or proper way to do this? I know ||=is not valid syntax. 2 obvious ways to handle it are:

我现在需要在 JavaScript 中做一些类似的事情。这样做的惯例或正确方法是什么?我知道||=是无效的语法。处理它的两种明显方法是:

window.myLib = window.myLib || {};

// or

if (!window.myLib)
  window.myLib = {};

回答by Dzung Nguyen

Both are absolutely correct, but if you are looking for something that works like ||=in ruby. The first method which is variable = variable || {}is the one you are looking for :)

两者都是绝对正确的,但是如果您正在寻找类似于||=ruby 的东西。第一种方法就是variable = variable || {}您正在寻找的方法:)

回答by Moritz Roessler

You can use the logical OR operator ||which evaluates its right operand if lValis a falsy value.

||如果lVal是假值,您可以使用逻辑 OR 运算符来评估其正确的操作数。

Falsy values include e.g null, false, 0, "", undefined, NaN

假值包括例如 null, false, 0, "", undefined, NaN

x = x || 1

回答by Inkling

If you're working with objects, you can use destructuring (since ES6) like so:

如果您正在处理对象,则可以像这样使用解构(ES6 起):

({ myLib: window.myLib = {} } = window);

...but you don't gain anything over the accepted answer except confusion.

...但是除了困惑之外,您对接受的答案没有任何好处。

回答by Elias Zamaria

The operator you asked about has been proposed as a feature in JavaScript. It is currently at Stage 3, so it is not yet an official part of the language, but it will become accepted, with at most minor changes if they discover major unexpected problems.

您询问的运算符已被提议作为 JavaScript 中的一项功能。它目前处于Stage 3,所以它还不是该语言的官方部分,但它会被接受,如果他们发现重大的意外问题,最多只会做一些小的改动。

You can use it now using the plugin-proposal-logical-assignment-operators Babel plugin. I have never used that plugin, so I have no idea how well it works.

您现在可以使用plugin-proposal-logical-assignment-operators Babel 插件来使用它。我从来没有用过那个插件,所以我不知道它的效果如何。

回答by chris

Ruby's ||= operator short circuits assignment. It can be thought of like this:

Ruby 的 ||= 运算符短路赋值。可以这样想:

return a || a = b

So in javascript, this looks very similar:

所以在 javascript 中,这看起来非常相似:

return a || (a = b);

It seems as pointed out in the comments below however, that this literal ruby form is less efficient than the standard javascript idiom a = a || b.

然而,似乎在下面的评论中指出,这种文字 ruby​​ 形式的效率低于标准的 javascript 成语 a = a || 湾

For reference: http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html

供参考:http: //www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html