Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:35:33+00:00 2026-06-18T05:35:33+00:00

In this code if (direction === ‘up’) { for (key in elements) { if

  • 0

In this 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;
        }
    }
}

I’d like to abstract out this common idiom:

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

so I could simply write

manyElements(element, function () {
});

How would I write the function prototype as starting point. My guess would be.

function manyElements (elements, the_function) {
    for (key in elements) {
        if (elements.hasOwnProperty(key)) {
            the_function();
        }
    }
}

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?

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

I don’t prefer not to use an outside library, except for correlation. Per answer here is lodash implementation:

Snippet 1

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

      while (++index < length) {
        if (callback(collection[index], index, collection) === false) {
          break;
        }
      }
    } else {
      each(collection, callback, thisArg);
    }
    return collection;
  }

Snippet 2

var each = createIterator(eachIteratorOptions); 

Snippet 3

  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
    );
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:35:34+00:00Added an answer on June 18, 2026 at 5:35 am

    If you do this several times in your code, there is nothing wrong with abstracting it out. You almost have the answer already too: Just pass the parameters you need back into the function that you passed into manyElements. I’d go for passing both the key and the value, since you might need the key in some places (e.g. to change the assignment to something else).

    function manyElements (elements, the_function) {
        for (key in elements) {
            if (elements.hasOwnProperty(key)) {
                the_function(key, elements[key]);
            }
        }
    }
    

    Usage would be like this:

    if (direction === 'up') {
        manyElements(elements, function (key, value) {
            value.style.opacity = statics.elapsed / max_time;
        });
    } else if (direction === 'down') {
        manyElements(elements, function (key, value) {
            value.style.opacity = (max_time - statics.elapsed) / max_time;
        });
    }
    

    I wouldn’t call the function manyElements though, because that name doesn’t really explain what the function does. Something like forEachProperty might be more self-documenting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code shows frm01: $(document).ready(function() { $(#reg).click(function () { $(#frm01).show(slide, { direction: down },
If some one can point me in the right direction for this code for
So, I have this simple code $('#info_name'+index).show(slide, {direction: right}, 1000, function(){ $('#info_info'+index).show('slide',{direction: up}, 1000);
I've found this code snippet: [self setValue:direction forKey:@currentDirection]; CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@position];
Hey so i have this code : $('#album_cell' + currentCell).toggle( function() { $('#album_cell'+currentCell).flip({ direction:
I'm trying to get the direction property for the element. I have this code:
I'm new to javascript, and need some direction on how to build this code.
I have this code: document.getElementById('root').style.left = '<?php echo $page_position[$info[os]][$info[browser]][0]; ?>'; document.getElementById('root').style.top = '<?php echo
I have this code for a banner that will reveal a drop down section
I have this javascript code: if (direction !== leftnav) { // DO THINGS };

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.