by implicit · 11y ago · 0 comments
The main thing that bothers me is that the default behaviour is too permissive.
let a = [1,2,3,4,5];
let [b] = a; // ok. b receives 1
The equivalent Python will fail because there are "too many values to unpack." /* I can write code independent of getCheapestSellers() impl */
var [bestPrice, secondBest] = getCheapestSellers("product_name"); [bestPrice, secondBest, _*] = getCheapestSellers('product_name')Add: To clarify, the error shows up at runtime in Python. So you're going to need tests anyway. Just as in JS.
Btw (unrelated), you can do the same thing in JS as well [a, b, ...rest] = [1, 2, 3, 4, 5]
Keep in mind this also comes up in cases like:
foo, _, bar = something()
In general you can't avoid doing something like it, why "optimize" for this one special case?Even worse:
let [a, b, c, d] = [2];
a receives 2. b, c, and d receive undefined.