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

  • Home
  • SEARCH
  • 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 9050267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:36:38+00:00 2026-06-16T12:36:38+00:00

Possible Duplicate: Javascript closure inside loops – simple practical example Javascript infamous Loop problem?

  • 0

Possible Duplicate:
Javascript closure inside loops – simple practical example
Javascript infamous Loop problem?

I have a base function, that I want to control what ends up being like a “bumpbox”. My goal is to instantiate multiple instances of this and give each of the declared variables a custom config.

The object looks like this:

Project.Modules.bumpbox = function(in_trigger, container) {

    var config = {

        'speed': 500,
        'easing' : false,//will 
        'in_callback': false,
        'out_callback' : false,
        'visible': false,
        'out_trigger' : $('#bumpbox_out_trigger'),//this is set by default only change if you know what you are doing!
    };

    this.test = function() {

        //this should be the default function. 

    };

And then, from another file, I want to instantiate an instance like new Project.Modules.Bumpbox() and overwrite the test function.

var bumpbox_controllers = {

            "map" : new Project.Modules.bumpbox($('#navigation_top li.map'), $('.bumpbox.map')),
            "contact" : new Project.Modules.bumpbox($('#navigation_top li.contact'), $('.bumpbox.contact')),
            "about" : new Project.Modules.bumpbox($('#navigation_left li.about'), $('.bumpbox.about')),
            "team" : new Project.Modules.bumpbox($('#navigation_left li.team'), $('.bumpbox.team')),
            "careers" : new Project.Modules.bumpbox($('#navigation_left li.careers'), $('.bumpbox.careers')),
            "services" : new Project.Modules.bumpbox($('#navigation_left li.services'), $('.bumpbox.services'))
        };

and then I want to loop through each of those and set a custom test() function in each like this:

    bumpbox_controllers[i]['test'] = function() {

        alert(i);
    }

But when I run this code, it will switch all of the elements to the last i value called, in this case “service”, not giving each a unique element.

  • 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-16T12:36:39+00:00Added an answer on June 16, 2026 at 12:36 pm

    You seem to need a closure for your loop:

    for (var controller in bumpbox_controllers) {
        bumpbox_controllers[controller] = (function(i) {
            // creating a new context for i
            return function() {
                alert(i); // access the i in scope, not the controller
            }
        })(controller);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Javascript closure inside loops - simple practical example I have an array
Possible Duplicate: Javascript closure inside loops - simple practical example Javascript: closure of loop?
Possible Duplicate: Javascript closure inside loops - simple practical example I'm having a problem
Possible Duplicate: Javascript closure inside loops - simple practical example Please explain the use
Possible Duplicate: Javascript closure inside loops - simple practical example javascript variable scope/closure in
Possible Duplicate: Javascript closure inside loops - simple practical example I was trying to
Possible Duplicate: Javascript closure inside loops - simple practical example I add event handlers
Possible Duplicate: Javascript closure inside loops - simple practical example I'm creating a table
Possible Duplicate: Javascript closure inside loops - simple practical example Calling setTimeout function within
Possible Duplicate: Javascript closure inside loops - simple practical example I am using webservice

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.