Prototype通用性方法-invoke(methodName[, arg...]) -> Array
Posted on 2008-04-08 13:51 尹合磊 阅读(508) 评论(0) 编辑 收藏 举报
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.
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.
1 ['hello', 'world', 'cool!'].invoke('toUpperCase')
2 // ['HELLO', 'WORLD', 'COOL!']
3
4 ['hello', 'world', 'cool!'].invoke('substring', 0, 3)
5 // ['hel', 'wor', 'coo']
6
7 // Of course, this works on Prototype extensions (why shouldn't it?!)
8 $('navBar', 'adsBar', 'footer').invoke('hide')
2 // ['HELLO', 'WORLD', 'COOL!']
3
4 ['hello', 'world', 'cool!'].invoke('substring', 0, 3)
5 // ['hel', 'wor', 'coo']
6
7 // Of course, this works on Prototype extensions (why shouldn't it?!)
8 $('navBar', 'adsBar', 'footer').invoke('hide')