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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:08:58+00:00 2026-06-04T19:08:58+00:00

This solution works, but I don’t understand what the second return function() does? for

  • 0

This solution works, but I don’t understand what the second return function() does?

for (var i = 0; i < photos.length; i ++) {
    img.onclick = (function(photo) {
        return function() {
            hotLink(photo); //window.location = '/pics/user/' + photo.user_id;  
        };  
    })(photos[i]);

Also, why do I have to include the (photos[i]); at the end?

Before, I had this, and the onclick would always link to the last photo[i].

  for (var i = 0; i < photos.length; i ++) {
      img.onclick = function() {
          window.location = 'pics/user/' + photo.user_id
      };
  }


   
  • 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-04T19:09:00+00:00Added an answer on June 4, 2026 at 7:09 pm

    When you do this (assuming there’s a photo = photos[i] there that you left out in your question):

    img.onclick = function() { window.location = 'pics/user/' + photo.user_id };
    

    The variable photo inside the function refers to the same variable as photo outside the function. It’s not a snapshot that gets the current value of the variable at the time you define the function; it’s just a reference to the same variable. The surrounding loop changes the value of that variable on every iteration, but it doesn’t create a new variable each time; it’s reusing the same one. So all the functions you generate reference that exact same variable – the one and only photo.

    By the time anyone actually clicks on the image and calls the function, the loop has long since terminated, and photo is gone from the main program’s scope, but it’s still out there in memory because all those functions still have references to it. And they will find it still pointing to the last item in the list, because that was the last thing assigned to it.

    So you need to give each onclick function its very own variable that won’t change once the function is created. The way to do that in Javascript, since it doesn’t have block scope, is to call a function and pass the value in as a parameter. Function parameters and variables declared inside a function (as opposed to photo in the non-working example above, which is used inside the function but declared outside it) are created fresh on every function invocation. When photo is declared as a function parameter, each onclick gets its very own copy that nothing else can modify, so it still has the right value when someone finally clicks the image.

    It might be clearer if it used a static function-generator function; there’s really no reason to do the inline declare-and-call thing. You could declare this once, outside the loop:

    function makeOnclick(somePhoto) {
        return function() { hotlink(somePhoto); }
    }
    

    And then the loop body could do this:

    img.onclick = makeOnclick(photo)
    

    You’re calling makeOnclick and passing it photo as a parameter. The makeOnclick function is declared far away, where it couldn’t use photo directly even if you wanted it to; it can’t see that variable at all. Instead, all it has is its local parameter somePhoto – which is created as a brand new variable every time you call makeOnclick. It’s initialized with the value of photo at the point of the call, but it’s just a copy, so when photo changes on the next loop iteration, that particular instance of somePhoto will stay the same. When the next iteration calls makeOnclick, it will create a new instance of somePhoto initialized to the new value of photo, and so on. So even though the inner function that makeOnClick is returning is inheriting the somePhoto var, that var was just created especially for that instance of makeOnClick; every one of those returned functions gets its own private somePhoto.

    Your working code above is doing exactly the same thing in a slightly different way. Instead of declaring the makeOnclick function once, outside the loop, and calling it a bunch of times, it’s redeclaring it every time through the loop as an anonymous function which it then calls immediately. This code:

    img.onclick = (function(x) { blah(x); })(photo);
    

    is the same as this:

    function foo(x) { blah(x); }
    img.onclick = foo(photo);
    

    without having to give the function a name. In JavaScript in general, this:

    (function (x,y,z) { doSomething(x,y,z); })(a,b,c);
    

    is the same as this:

    function callDoSomething(x,y,z) { doSomething(x,y,z); }
    callDoSomething(a,b,c);
    

    except the function has no name and is not saved anywhere; it goes away right after it’s called.

    So declaring the onclick-generator function every time through the loop and calling it immediately all at once is nice and concise, but not terribly efficient.

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

Sidebar

Related Questions

The recommended solution is this: config.active_record.whitelist_attributes = true But this only works if you
We have a reporting solution that works like this. User orders the report to
I can't find any solution to get this jquery events to work. $(window).one('click',function(){ console.log('hide
I have this mockup page. The accordion works well but I would like to
I found the answer to this question in here . that solution works great
On Local host,my solution works fine but on server when i deploy ,it gives
NARROWED DOWN SOLUTION I'm much closer, but don't know how to apply XAML to
I've tried all possible solutions to this problem, but nothing seems to work... The
I am trying to work this solution on how to collapse the records based
Is there any integrated solution for this? Would it work if I create a

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.