julio
julio

Reputation: 6728

strange syntax in javascript

I'm working on debugging some code someone else wrote (using Mootools as the base library), and I came across this function:

[note, $H(options.text).getKeys()].flatten().each(function(option){
  // bunch of stuff happening
});

I've never seen this syntax before, with the brackets and the $H notation (eg. [note, $H(options.text).getKeys()]). Can anyone explain how that works or point me to a reference on it?

Thanks!

Upvotes: 7

Views: 155

Answers (2)

Mike Christensen
Mike Christensen

Reputation: 91666

This basically aggregates two arrays together. Take, for example, this code:

var a = [1,2,3];
var b = [4,5,6];
var c = [a, b].flatten();
alert(c);

The arrays [1,2,3] and [4,5,6] are combined (or "flattened") into a single array, 1,2,3,4,5,6.

In your code:

[note, $H(options.text).getKeys()].flatten()

note (perhaps another array) and whatever getKeys() returns are flattened into a single array. Then, a function is performed across each element.

Update:

The $H function is a utility function in Mootools that is a shortcut for Hash().

Upvotes: 6

Joe
Joe

Reputation: 82624

[note, $H(options.text).getKeys()]

is most likely becoming:

[note, ["string1", "string2"]]

so it returns an array. So ["whatever note is", ["Another array", "of objects"]] needs to be flattened to:

["whatever note is", "Another array", "of objects"]

Upvotes: 1

Related Questions