javascript - Struggling to Understand the Parameter Content when using .map on Function Expressions -
so i'm looking @ function expressions.
this code i'm playing with:
var modifiednames = [ "thomas meeks", "gregg pollack", "christine wong", "dan mcgaw" ]; modifiednames.map(function(cheese) { alert("yo, " + cheese + "!"); }); it works fine i'm having trouble understanding part of it. grasp .map doing parameter throwing me. how parameter collecting array's information? called cheese that's go-to test word.
i have read couple of explanations i'm not grasping it, hoping here simplify explanation somewhat. thank you.
you're passing function .map(), runs loop, , invokes function on every iteration, passing current item.
think of this:
for (var = 0; < array.length; i++) { callback(array[i]); } here array array on called .map(), , callback function passed in.
this isn't mysterious now. it's calling function in loop.
of course, .map() more work , passes more args, shows how parameter gets populated.
a more complete implementation of map this:
array.prototype.mymap = function(callback, thisarg) { var result = new array(this.length); (var = 0; < this.length; i++) { if (i in this) { result[i] = callback.call(thisarg, this[i], i, this); } } return result; };
Comments
Post a Comment