0

Long and short of the story is, whilst reading John Resig's blog (specifically http://ejohn.org/blog/javascript-trie-performance-analysis/) I came across a line which makes absolutely no sense to me whatsoever. Essentially it boils down to

object = object[key] = something;

(this can be found in the first code block of the article I've linked.)

This has proven rather difficult to google, so if anyone can offer some insight / a good online resource for me to learn for myself, I'd much appreciate it.

To claify on a single point, the object in the multiple assignment is the same ie.

A = A[key] = something;

Perhaps my question should be, what's the point in doing this?

Ben Emery
  • 128
  • 8
  • [http://stackoverflow.com/a/1758912/1225328](http://stackoverflow.com/a/1758912/1225328) – sp00m Sep 24 '12 at 09:40

4 Answers4

0

The line assigns something to object[key] and object. You can read it like so:

object[key] = something;
object = object[key];
zeebonk
  • 4,864
  • 4
  • 21
  • 31
0

It is called Bracket notation

Properties of JavaScript objects can also be accessed or set using a bracket notation. Objects are sometimes called associative arrays, since each property is associated with a string value that can be used to access it. So, for example, you could access the properties of the myCar object as follows:

myCar["make"] = "Ford";
myCar["model"] = "Mustang";
myCar["year"] = 1969;

UPDATE Ah, I missed the two equals. - multiple assignment as mentioned in another answer

NOTE: It is unlikely that you would want

object=object[somekey]=something

since it would overwrite the object you just used in the middle but rather

objectA=objectB[somekey]=something

where objectA would be the same type as objectB[somekey] and something

mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

It's called multiple assignment.

Let's make an example:

a = b = 5;

After this instruction the value of both a and b is 5.

It's like doing:

a = 5;
b = 5;
napolux
  • 15,574
  • 9
  • 51
  • 70
0

It's a combination of multiple assignment and bracket notation.

You could also read it like this:

// Key is a string value!
var key = 'foobar';
object[key] = something;    
object = something;


// Or, if key is known
object.foobar = something;
object = something;
SvenFinke
  • 1,254
  • 3
  • 15
  • 30