javascript - Call function for all array elements without passing item as parameter -
in javascript can this:
var show = function(w) { alert(w); }; var words = ['a', 'b', 'c']; words.foreach(show);
note don't need pass array item parameter.
is there way same in ruby?
like:
def show(w) puts w end words = ['a', 'b', 'c'] words.each(show)
ps: know can doing: words.each { |w| show(w) }
. question if can in javascript, without passing item parameter.
sure, passing method, in ecmascript:
words.each(&method(:show))
note way make ruby example work. however, ruby method not closest analogue ecmascript function in ruby. proc
closer match:
show = -> w { puts w } words.each(&show)
this reads close ecmascript 6 version:
const show = w => alert(w); words.foreach(show);
Comments
Post a Comment