7

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!

julio
  • 6,630
  • 15
  • 60
  • 82
  • 1
    `[]` are an Array literal. `$H()` is calling a function named `$H`. No different than `var myarray = []; myarray[0] = 'some val'; myarray[1] = some_func('some arg').some_other_func();` – RightSaidFred Nov 30 '11 at 19:14

2 Answers2

6

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().

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • thanks for the explanation. It seems obvious now. I think coming from a more PHP background that `$H` really threw me off. I don't recall ever seeing that in JS. Is there a reason to use that rather than just the function name? – julio Nov 30 '11 at 19:19
  • 1
    I don't really know Mootools that well, but `$` is a perfectly valid name for a function in Javascript. jQuery uses it for its main selector function. I'm not sure what the `$H` function is or what defined it (it almost looks like minified or crunched code?) - Maybe some Mootools people can chime in. – Mike Christensen Nov 30 '11 at 19:23
  • @Mike-- I added some more info in the original post with the only other usage of `$H` in the script. – julio Nov 30 '11 at 19:53
  • I looked it up. `$H` is a Mootools shortcut for `Hash()` - http://mootools.net/docs/more/Types/Hash#H – Mike Christensen Nov 30 '11 at 19:57
  • For the sake of completeness :) .. `flatten` is actually a method that flattens multidimensional arrays => http://mootools.net/docs/core/Types/Array#Array:flatten ..tbh in your answer, it would be better to use the `append` method ;) – stecb Nov 30 '11 at 19:58
  • Yikes, every time I see a $ function in Mootools, like julio, I cringe a bit, mostly because I've become acclimatised to not seeing `$` anywhere in javascript unless PHP is involved. Although other `$...` functions have been deprecated, or replaced with other names, so I'd be wary of using this as a shortcut. (I could also be completely wrong, by the way.) The specific example I am thinking about here is `$clear`, having been deprecated in favour of `clearTimeout();` – Julian H. Lam Nov 30 '11 at 20:55
  • OK so $H has actually be deprecated, it is available in more or in core w/ compatabilty the reason being is simple From mooTools Type: Hash. Hash has been deprecated. Each Hash method has a similar Object method or a Vanilla JS equivalent. This has been true since v1.3's launch – Tim Wickstrom Nov 30 '11 at 21:37
  • quite. Object generics have made Hash redundant, the reason it existed was to be able to get the effect of prototyping Object w/o doing so. If you want a data table, there's a Table type - else, use Object now. – Dimitar Christoff Dec 01 '11 at 07:23
1
[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"]
Joe
  • 80,724
  • 18
  • 127
  • 145