I’m wondering if there is a better way to handle multiple function calls. Basically I have a function which receives and element as one of the arguments, along with some properties. I have to call this function on several elements…
function positionMe(element,position){ ... }
positionMe(obj1,top);
positionMe(obj2,left);
positionMe(obj3,bottom);
positionMe(obj4,right);
positionMe(obj5,bottom);
Just looking at the code seems like I’m doing something really wrong. At first I thought I might be able to pass a collection of elements in, but it doesn’t seem to work. Any ideas?
With the information you have provided I would create an array with all the needed information and loop through it inside your function:
UPDATE
As Reflective commented:
This is not the case for arrays in javascript. Well it’s not that simple. because you will end up also looping through all the inherited stuff of the array object like e.g.
pop(),push()etc. There is a way to prevent this though through the use ofhasOwnProperty().Another way would have been to use
forEach()which is available as of JavaScript 1.6. So I don’t think all user agents already builtin support for this.