简体   繁体   English

JavaScript object 文字与 Python 字典是否相同?

[英]Are JavaScript object literals the same thing as Python dictionaries?

I've started using JavaScript recently and have noticed a resemblance between JavaScript object literals and python dictionaries.我最近开始使用 JavaScript 并注意到 JavaScript object 文字和 python 字典之间的相似之处。 They are used in similar situations and have similar syntax.它们用于相似的情况并且具有相似的语法。 So are they basically the same thing but with different names?那么它们基本上是同一种东西,只是名称不同吗?

They have the same structures;它们具有相同的结构; key: value . key: value The key could be an array, integer, object etc. Same goes for the value.键可以是一个数组,integer、object 等。值也是如此。

Python: Python:

my_python_dictionary = {'name': 'John', age: 5, 123: 'a string'}
one_of_the_values_of_my_python_dictionary = my_python_dictionary['name']

JavaScript: JavaScript:

let myJavaScriptObjectLiteral = {"name": "John", age: 5, 123: "a string"}
let oneOfTheValuesOfMyJavaScriptObjectLiteral = myJavaScriptObjectLiteral["name"]

it is totally not the same thing.这完全不是一回事。 in JS, object literals is a object, so you can combine them with data and function, and you can call the method by dot, just like x.xxx(), and you can deconstruction it with the same symbol {...}, just like other object.在JS中,object字面量是一个object,所以可以和data以及function结合起来,可以像x.xxx()一样通过点调用方法,同样可以用符号{...} ,就像其他 object 一样。

but in python, dict is not a object, you can not use dot, and if you want deconstruction, you must use ** for it, by the way, you can deconstruction turple by *但是在python中,dict不是object,不能用dot,如果要解构,必须用**,顺便说一句,可以通过*解构turple

summarize: their is nothing just like object literals in python...总结:它们与 python 中的 object 文字完全不同......

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

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