Strange syntax in javascript

I am working on debugging code that someone 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 have never seen this syntax before using brackets and $ H notation (for example, [note, $H(options.text).getKeys()] ). Can someone explain how this works or point me to a link to it?

Thanks!

+7
source share
2 answers

It basically combines two arrays. Take for example this code:

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

Arrays [1,2,3] and [4,5,6] combined (or "flattened") into one array, 1,2,3,4,5,6 .

In your code:

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

note (possibly another array), and all getKeys() returns are smoothed into a single array. Then through each element a function is executed.

Update:

The $ H function is a utility function in Mootools; this is a shortcut to Hash ().

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

most likely it will become:

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

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

 ["whatever note is", "Another array", "of objects"] 
+1
source

All Articles