arrays - Javascript Objects: Using Arguments as object property -
i reimplementing memoize function using arguments object. wondering why work. example, passing in 2 arguments:
var args = array.prototype.slice.call(arguments); // input: 1,2 then store args array object. example,
var obj = {}; obj[args] = 2; if call object, see as:
{ 1,2: 2 }/*shows this*/ { [1,2]: 2 }/*but not this*/ not wanted second object, curious happening under hood. call javascript coercion?
object property names strings. when do:
obj[args] = 2; it treats as
obj[args.tostring()] = 2; and tostring() method of arrays equivalent .join(","), above equivalent to:
obj[args.join(",")] = 2; which matches result saw.
Comments
Post a Comment