arrays - How does object[[["key"]]] evaluate to object["key"] in javascript? -
this question has answer here:
why javascript evaluate following true, given object foo
has valid property bar
?
foo[[[["bar"]]]] === foo["bar"]
based on operator precedence, think foo[[[["bar"]]]]
trying access property array [[["bar"]]]
key, why still "flatten down" same foo["bar"]
?
colleagues of mine saying javascript parsers have bracket simplifying ignores brackets. don't think true since saving [[["foo"]]]
variable test
gives same result:
> test = [[["bar"]]] [array[1]] > foo["bar"] = 5 5 > foo[test] 5
what aspect of language or parser causing behavior? thanks!
javascript bracket notation accepts expression, converts value of expression string. if pass in array, attempt convert string. in case passing in array [[["bar"]]]
, , [[["bar"]]].tostring() === "bar"
.
if wondering why [[["bar"]]].tostring() === "bar"
, because when array arr
converted string implicitly calling arr.join(',')
. each of elements converted strings , joined in comma separated string. when array has 1 element, string representation of array string representation of 1 element. in case array ([[["bar"]]]
) has 1 element: [["bar"]]
.
that array converted string too, , since 1 element array, string representation of string representation of single element: ["bar"]
.
["bar"]
array 1 element, string, string representation of ["bar"]
"bar"
.
what comes down is: [[["bar"]]].tostring() === "bar"
and foo[[[["bar"]]]]
same foo[[[["bar"]]].tostring()]
.
you find that:
foo[[[[1]],[2]]] === foo["1,2"]
because: [[[1]],[2]].tostring() === "1,2"
.
Comments
Post a Comment