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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:45:01+00:00 2026-06-08T00:45:01+00:00

Are global variables accessible in jquery nested functions? Im having an issue that i

  • 0

Are global variables accessible in jquery nested functions?
Im having an issue that i cant solve. One object property changes and another one doesn’t

The situtation is this

    for(var key in object) {
        if(object[key].boolean) {
            alert(object[key].value)
            $.get(url, "data", function(data) {
                if(parseInt(data) > object[key].int) {
                    alert(object[key].value);
                    object.int = data;
                }
            });
        }
    }

The issue i am having is the object[key].value does not hold its value. Both alerts have different values so i cant use it in the nested return function. Also object[key].int gets updated correctly which is more confusing. Object is declared as a global variable.

Any suggestions is greatly appreciated.

  • 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-08T00:45:04+00:00Added an answer on June 8, 2026 at 12:45 am

    your anonymous wrapper is called only after get becomes success,it is not called right away, looks like some situation like

    Return a function from the anonymous wrapper?

    are you getting the last item alerted from the inside closure?

    var object = {
        x:10,
        y:20,
        z:30
    };
    for(var key in object) {
                console.log("outside closure",object[key]);
                    $.get("/echo/html?data=asdf" ,(function(keyAlias) {
                        return function(data) {
                            console.log("inside closure",object[keyAlias],data);
                            //your logis goes here
                        }
                     })(key));
        }
    

    Fiddle : http://jsfiddle.net/eTdv5/1/ (check console logs)

    Explaining the whole thing is not becessary as I guess, you can check the above mentioned SO post, In case of anything unclear please ask.

    Explanation:

    First, key = 10

    $.get("/echo/html?data=asdf" , <at this place key is 10> );
    

    (function(key) {})(key) is like calling display(key), think that you are writing display function right away and calling it with an argument key.

    return function(data) {}; will return a function, in effect think like (function(key) {})(key) getting replaced with this returned function.

    That will give you an introduction, now how the scope is managed:

    The key will change its value as the for in loop loops through object, but each callback function we created has its own keyAlias assosiated with it which corresponds to the value of key at the time callback function was crated using (function(key) {})(key)

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

Sidebar

Related Questions

I have a global variables that I am using as a default variable. Depending
How would I add a Stack object into my global.asax so it's accessible by
I've heard from a variety of places that global variables are inherently nasty and
I want some variables to be global across the project and accessible in every
How do you define a global variable that is accessible across all views on
I have a VB application, that uses some global variables to store data that
Possible Duplicate: Global Variables in Cocoa/Objective-C? problem with declare a global variable in objective
The discussion around global variables and their misuse seems to hold a certain dogmatic
I always hear using global variables are dangerous. Does this apply to Drupal? Take
I'm using global variables to pass around a response from a AJAX call: window.response

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.