Abstracting out a common idiom
<p>In this code</p>

<pre><code>if (direction === 'up') {
    for (key in elements) {
        if (elements.hasOwnProperty(key)) {
            elements[key].style.opacity = statics.elapsed / max_time;
        }
    }
} else if (direction === 'down') {
    for (key in elements) {
        if (elements.hasOwnProperty(key)) {
            elements[key].style.opacity = (max_time - statics.elapsed) / max_time;
        }
    }
}
</code></pre>

<p>I'd like to abstract out this common idiom:</p>

<pre><code>for (key in elements) {
    if (elements.hasOwnProperty(key)) {
        // function using elements[key] and outside parameters
    }
}
</code></pre>

<p>so I could simply write </p>

<pre><code>manyElements(element, function () {
});
</code></pre>

<p>How would I write the function prototype as starting point.  My guess would be.</p>

<pre><code>function manyElements (elements, the_function) {
    for (key in elements) {
        if (elements.hasOwnProperty(key)) {
            the_function();
        }
    }
}
</code></pre>

<p>but I need access to outside parameters, what is the best way to pass these in?  Is this a good idea in general, to abstract out this code?</p>

<p>It seems like I would have to pass each parameter in individually defeating the purpose of making a general purpose function.</p>

<p>I don't prefer not to use an outside library, except for correlation.  Per answer here is lodash implementation:</p>

<p><strong>Snippet 1</strong></p>

<pre><code>  function forEach(collection, callback, thisArg) {
    if (callback &amp;&amp; typeof thisArg == 'undefined' &amp;&amp; isArray(collection)) {
      var index = -1,
          length = collection.length;

      while (++index &lt; length) {
        if (callback(collection[index], index, collection) === false) {
          break;
        }
      }
    } else {
      each(collection, callback, thisArg);
    }
    return collection;
  }
</code></pre>

<p><strong>Snippet 2</strong></p>

<pre><code>var each = createIterator(eachIteratorOptions); 
</code></pre>

<p><strong>Snippet 3</strong></p>

<pre><code>  function createIterator() {
    var data = {
      'arrayLoop': '',
      'bottom': '',
      'hasDontEnumBug': hasDontEnumBug,
      'isKeysFast': isKeysFast,
      'objectLoop': '',
      'nonEnumArgs': nonEnumArgs,
      'noCharByIndex': noCharByIndex,
      'shadowed': shadowed,
      'top': '',
      'useHas': true
    };

    // merge options into a template data object
    for (var object, index = 0; object = arguments[index]; index++) {
      for (var key in object) {
        data[key] = object[key];
      }
    }
    var args = data.args;
    data.firstArg = /^[^,]+/.exec(args)[0];

    // create the function factory
    var factory = Function(
        'createCallback, hasOwnProperty, isArguments, isString, objectTypes, ' +
        'nativeKeys, propertyIsEnumerable',
      'return function(' + args + ') {\n' + iteratorTemplate(data) + '\n}'
    );
    // return the compiled function
    return factory(
      createCallback, hasOwnProperty, isArguments, isString, objectTypes,
      nativeKeys, propertyIsEnumerable
    );
  }
</code></pre>
