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 3947394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:14:46+00:00 2026-05-20T01:14:46+00:00

I want to set a function as a property of each element in an

  • 0

I want to set a function as a property of each element in an array, but call it with different arguments. I thought I’d solve it using an anonymous function:

for ( var i = 0; i < object_count; i++ ) {
    objects[i].callback = function(e,x,y){ cb(e,x,y,i) };
}

However, the function is called with the value that i has at the time. How would I preserve the context?

  • 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-05-20T01:14:47+00:00Added an answer on May 20, 2026 at 1:14 am

    You can wrap the assignment in a function, or the right-hand side at least:

        objects[i].callback = (function(i) { return function(e, x, y) {
          cb(e, x, y, i);
        })(i);
    

    The “outer” anonymous function is called right away, copying the loop variable “i” into an argument (which I also called “i”; some think it’s confusing to do that while others say it’s confusing not to do it, so you can decide 🙂 which is then used by the returned “inner” anonymous function.

    Another approach to this problem would be to use a utility function for this purpose instead of a simple in-line anonymous function. In this case, it’s made slightly tricky because you want “i” to be the last parameter of the actual callback function. The Functional JavaScript library has a cool utility that lets you “pre-populate” selected arguments of a function with fixed values, giving you a function that lets you pass in arguments to the non-fixed ones; it’d look something like this:

      objects[i].callback = (function(e, x, y, i) { return cb(e, x, y, i); }).partial(_, _, _, i);
    

    Whether that’s better or worse is a matter of style and opinion.

    edit just had a little more coffee – I think that I was being a little silly thinking that I had to use “partial()” above. The fact that the inner (the “real” function) wants “i” at the end of the parameter list is really not relevant to the way things need to be set up. That above example could also be done like this:

       objects[i].callback = (function(i, e, x, y) { return cb(e, x, y, i); }).curry(i);
    

    which is way less bizarre. (Either would work however. At least, I think they would. 🙂

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

Sidebar

Related Questions

I want to return a set of values from function till the point they
I want to create a postgres function that builds the set of columns it
I want set Listbox background to transparent but not working Is there any idea?
i want set class for repeater when doPostBack for those dataID i wan but
I want to use magic function __set() and __get() for storing SQL data inside
I want to make sure that a set of functions have the same signature
I want set interfaceOrientation in one UIView as LandscapeLeft and Portrait in other UIView.
i want set the visibility to itemized overlay in map view. if the zoom
My question is : I have an EditText and I want set a margin
i have a problem, i want set text of a UILabel or UItextView or

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.