简体   繁体   中英

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. 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 . The key could be an array, integer, object etc. Same goes for the value.

Python:

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

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.

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 *

summarize: their is nothing just like object literals in python...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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