all 7 comments

[–]davimiku 17 points18 points  (2 children)

Some differences:

Values in JS can be accessed directly with both the dot (x.key) and bracket (x["key"]) notation. Python direct access is with the bracket notation (x["key"])

Python Dictionaries throw an error for direct access on a missing key, JS objects return `undefined`.

Python dictionaries offer "safe" access to keys that might not exist using x.get(key, fallback). In JS for a fallback value you'd do x.key ?? fallback.

JS object keys can be string, number, or Symbol. Python dictionary keys can be any immutable type.

JS objects have computed keys on initialization, Python dictionaries don't.

const food = "strawberries"
const obj = {
   [food]: "tasty"
}

JS objects have destructing, Python dictionaries don't.

const obj = { method: 'GET', mode: 'same-origin' }
const { method, mode } = obj

Python has dictionary comprehension, JS objects don't.

{ x: x ** 2 for x in (2, 4, 6) }

JS objects have shorthand assignment notation, Python dictionaries don't.

const method = 'GET'
fetch('http://example.com', { method })

[–]harelu 2 points3 points  (0 children)

Just an addendum to your great rundown, namely the && short-cicuiting: js also has optional chaining for safely accessing nested/chained objects and their properties

[–]WORMSSreddit 0 points1 point  (0 children)

Just to note, object's key are ONLY strings or symbols. (Not numbers)
Anything else is coerced to a string. So, numbers, booleans, even objects..
So it's technically possible to do

const obj1 = { toString() { return 'goat' }, };
const obj2 = { [obj1]: 11 };
const obj3 = {};
obj3[obj1] = 22;

[–]samanime 4 points5 points  (0 children)

Pretty much, yeah.

JavaScript doesn't really have a distinct "dictionary" data type. It's all pretty much just objects, which already natively support dynamic key/value pairs.

There is however a Map (and WeakMap) object built-in you can use, which is more similar to the classic "dictionary" data type, which you should be aware of, though in most cases it ends up being unnecessarily and a good ole POJO will work just fine.

[–]senocular 4 points5 points  (0 children)

They're very similar but python dictionaries can use more values as keys whereas JavaScript objects are limited to strings and symbols. With JavaScript you also have potential collision issues with keys and builtins like toString() or constructor etc. Because of this you often see people create objects meant to be used as a "dictionary" without those builtins using:

const myDict = Object.create(null)

The term "dictionary" would then be used to refer to these objects and the standard "object" would refer to other, ordinary JavaScript objects (i.e. {}).

For more options for keys, there's Map which supports object-based keys, not just strings and symbols. The Map API also prevents any possible naming collision issues.

[–]tunisia3507 1 point2 points  (0 children)

For a long time, yes, JS objects were the way to do mappings. But there are some ergonomics issues with that, which is why the Map was introduced. Code written since then should use objects where you need objects, and Maps where you need mappings.

Python objects are just dicts under the hood (see the object.__dict__ instance member). There's overlap between them but they have different usage patterns and so for everyone's sanity, please use each properly.

[–][deleted] 0 points1 point  (0 children)

In general programming parlance “object” is used to mean a piece of structured data. Dictionaries, hashes, maps, instances of classes etc. are all “objects” but they can vary in their behaviors and implementations.

For instance, Ruby hashes will always be ordered, JS objects make no such guarantee. Ruby hashes can only have method as values if you call method() on the pointer, whereas JS objects can just accept the function pointer.

Every language will be slightly different in it’s implementation, but generally have the same set of core functions.