来自元组的JavaScript变量赋值 [英] JavaScript Variable Assignments from Tuples

查看:417
本文介绍了来自元组的JavaScript变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 2和Python 3等其他语言中,您可以为元组变量定义和赋值,并检索它们的值,如下所示:

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

JavaScript中有类似内容吗?或者我只需要用数组的丑陋方式来做:

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

有没有更好的方法在JavaScript 5中模拟Python元组?

Is there a better way to simulate Python tuples in JavaScript 5?

推荐答案

你必须这样丑陋的方式。如果您真的想要这样的东西,您可以查看 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.)

这篇关于来自元组的JavaScript变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆