Tuesday, 15 March 2011

javascript - How to iterate through a nodeList functional style -



javascript - How to iterate through a nodeList functional style -

i want iterate through nodelist foreach or map. simplified code works this:

var nodelistmap = array.prototype.map; var els = document.queryselectorall('.classname'); nodelistmap.call(els, function(el){...});

this works fine. however, i'd prefer not have map.call, if this...

var nodelistmap = array.prototype.map.call; var els = document.queryselectorall('.classname'); nodelistmap(els, function(el){...});

then returns

typeerror: object not function

how can modify code nodelistmap(array, fn)?

it's simplest write own function "does right thing"

function map() { var args = [].slice.call(arguments, 0); var ctx = args.shift(); homecoming [].map.apply(ctx, args); }

which work any pseudo-array object.

edit code updated ensure all arguments passed .map, if ecma add together more in future.

javascript prototype

No comments:

Post a Comment