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

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -