Reputation: 4465
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?
Upvotes: 2
Views: 1154
Reputation: 743
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...
Upvotes: 1
Reputation: 103
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"]
Upvotes: 0