JavaScript 元组中的变量赋值
在 Python 2 和 Python 3 等其他语言中,您可以定义元组变量并将值分配给元组变量,然后像这样检索它们的值:
tuple = ("Bob", 24)
name, age = tuple
print(name) #name evaluates to Bob
print(age) #age evaluates to 24
JavaScript 中有类似的东西吗?或者我是否必须用数组以丑陋的方式做到这一点:
tuple = ["Bob", 24]
name = tuple[0] //name Evaluates to Bob
age = tuple[1] //age Evaluates to 24
是否有更好的方法在 JavaScript 5 中模拟 Python 元组?
更新:请参阅有关 ES6 的答案,对于新项目来说,ES6 应该比 CoffeeScript 更受青睐。
In other languages like Python 2 and Python 3, you can define and assign values to a tuple variable, and retrieve their values like this:
tuple = ("Bob", 24)
name, age = tuple
print(name) #name evaluates to Bob
print(age) #age evaluates to 24
Is there anything similar in JavaScript? Or do I just have to do it the ugly way with an array:
tuple = ["Bob", 24]
name = tuple[0] //name Evaluates to Bob
age = tuple[1] //age Evaluates to 24
Is there a better way to simulate Python tuples in JavaScript 5?
Update: See the answer regarding ES6, which should be favored over CoffeeScript for new projects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
JavaScript 1.7 添加了解构赋值语法,它允许你基本上要做你想做的事:
JavaScript 1.7 added destructuring assignment syntax which allows you to do essentially what you are after:
你必须以丑陋的方式去做。如果您真的想要这样的东西,您可以查看CoffeeScript,它有这个和一个还有很多其他功能使它看起来更像 python(抱歉让它听起来像广告,但我真的很喜欢它。)
You have to do it the ugly way. If you really want something like this, you can check out CoffeeScript, which has that and a whole lot of other features that make it look more like python (sorry for making it sound like an advertisement, but I really like it.)
您可以执行类似的操作:
然后将名称和年龄引用为属性
You can do something similar:
and then refer to name and age as attributes
这种“元组”功能在 EcmaScript2015 中称为解构,并且很快就会受到最新浏览器的支持。目前,只有 Firefox 和 Chrome 支持。
但是,嘿,您可以使用 转译器。
代码看起来和 python 一样漂亮:
This "tuple" feature it is called destructuring in EcmaScript2015 and is soon to be supported by up to date browsers. For the time being, only Firefox and Chrome support it.
But hey, you can use a transpiler.
The code would look as nice as python:
冻结数组的行为与 python 元组相同:
花哨并定义一个类
现在可以在所有最新的浏览器中使用。
A frozen array behaves identically to a python tuple:
Be fancy and define a class
Works today in all the latest browsers.
JavaScript 不支持元组
如果您正在寻找不可变列表,可以使用 Object.freeze() 使数组不可变。
来源:Mozilla 开发者网络 - 对象。 freeze()
照常分配一个数组,但使用“Object.freeze()”锁定它
像使用常规数组一样使用这些值(python 多重赋值)不支持)
尝试分配新值
但值未更改
Tuples aren't supported in JavaScript
If you're looking for an immutable list, Object.freeze() can be used to make an array immutable.
Source: Mozilla Developer Network - Object.freeze()
Assign an array as usual but lock it using 'Object.freeze()
Use the values as you would a regular array (python multi-assignment is not supported)
Attempt to assign a new value
But the value is not changed
不幸的是,您不能在 (ECMA|Java)Script 中使用该元组赋值语法。
编辑:有人链接到 Mozilla/JS 1.7 - 这不能跨浏览器工作,但如果不需要,那么这就是你的答案。
Unfortunately you can't use that tuple assignment syntax in (ECMA|Java)Script.
EDIT: Someone linked to Mozilla/JS 1.7 - this wouldn't work cross-browser but if that is not required then there's your answer.
这并不是为了在现实生活中实际使用,只是一个有趣的练习。请参阅 为什么使用 JavaScript eval 函数是一个坏主意? 了解详情。
这是您无需求助于特定于供应商的扩展即可获得的最接近的结果:
辅助函数:
证明它可以在任意范围内工作:
Safari 不支持
eval
。This is not intended to be actually used in real life, just an interesting exercise. See Why is using the JavaScript eval function a bad idea? for details.
This is the closest you can get without resorting to vendor-specific extensions:
Helper function:
Proof that it works in arbitrary scope:
eval
is not supported in Safari.作为部长答案的更新,您现在可以使用 es2015 执行此操作:
https://jsbin.com /fubaluwimo/edit?js,控制台
As an update to The Minister's answer, you can now do this with es2015:
https://jsbin.com/fubaluwimo/edit?js,console
您也可以在 Javascript 中使用元组类型。用高阶函数定义就可以了(学术术语是Church编码):
请注意,
Tuple
不用作普通构造函数。该解决方案根本不依赖于原型系统,而仅依赖于高阶函数。与像元组一样使用的
Array
相比,元组有哪些优点? Church 编码的元组在设计上是不可变的,因此可以防止突变引起的副作用。这有助于构建更强大的应用程序。此外,更容易推理区分作为集合类型的Array
(例如[a]
)和作为各种类型的相关数据的元组(例如>(a, b)
)。You can have a tuple type in Javascript as well. Just define it with higher order functions (the academic term is Church encoding):
Please note that
Tuple
isn't used as a normal constructor. The solution doesn't rely on the prototype system at all, but solely on higher order functions.What are the advantages of tuples over
Array
s used like tuples? Church encoded tuples are immutable by design and thus prevent side effects caused by mutations. This helps to build more robust applications. Additionally, it is easier to reason about code that distinguishes betweenArray
s as a collection type (e.g.[a]
) and tuples as related data of various types (e.g.(a, b)
).这是一个简单的 Javascript 元组实现:
这是一个 2 元组,但是,您可以修改我的示例以支持 3、4、5、6 等元组。
Here is a simple Javascript Tuple implementation:
This is a 2-tuple, however, you could modify my example to support 3,4,5,6 etc. tuples.
我做了一个效果很好的元组实现。该解决方案允许数组解构以及基本类型检查。
I made a tuple implementation that works quite well. This solution allows for array destructuring, as well as basic type-cheking.