invoke

invoke(methodName[, arg...]) -> Array

Optimization for a common use-case of each or collect: invoking the same method, with the same potential arguments, for all the elements. Returns the results of the method calls.

Since it avoids the cost of a lexical closure over an anonymous function (like you would do with each or collect), this performs much better.

Perhaps more importantly, it definitely makes for more concise and more readable source code.

Examples


['hello', 'world', 'cool!'].invoke('toUpperCase')
// ['HELLO', 'WORLD', 'COOL!']

['hello', 'world', 'cool!'].invoke('substring', 0, 3)
// ['hel', 'wor', 'coo']

// of course, this works on Prototype extensions
$('navBar', 'adsBar', 'footer').invoke('hide')

// because Prototype extensions return the element itself,
// you can also chain 'invoke' calls:
$$('#windows div.close').invoke('addClassName', 'active').invoke('show');

See also

The pluck method does much the same thing for property fetching.