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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:15:46+00:00 2026-05-25T22:15:46+00:00

I need some help understanding scope in Javascript. I posted a similar question earlier

  • 0

I need some help understanding scope in Javascript. I posted a similar question earlier and prematurely marked it as accepted. (The solution didn’t work in my case).

I’ve got javascript (a) and jQuery script (b). I need to access some values from (a) in (b).

In (a) I’ve got:

function map_maker_js( args ) {
 var map = new google.maps.Map(document.getElementById( args['id'] ), myOptions);
//rest of the code for building the map goes here

google.maps.event.addListener(map, 'tilesloaded', viewport_bounds);

function viewport_bounds() {
    var bounds = map.getBounds();
    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();

    var maxLat = ne.lat();
    var maxLong = ne.lng();
    var minLat = sw.lat();
    var minLong = sw.lng();

    var the_maps_bounds = [maxLat, maxLong, minLat, minLong];

    //return(the_maps_bounds);

 }

}

In jQuery script (b), I need the_maps_bounds, which is calculated by viewport_bounds(). I can’t simply move google.maps.event.addListener(map, 'tilesloaded', viewport_bounds); into (b), because ‘map’ is out of scope. This won’t work in (b):

google.maps.event.addListener(map, 'tilesloaded',  function() {
    // This code will only execute after event `tilesloaded` happens

    var the_maps_bounds = viewport_bounds()

    // now do something with the bounds
    // ...
});

If I move the viewport_bounds() function outside of map_maker_js, then I’ll have to add map as an argument, which won’t work in (b), either.

The code that I’m working on is in a WordPress plugin. I don’t think that that will affect the answer, though.

I’m learning still learning javascript. What’s the best way to solve this?

Thank you


Update:

Thank you Rob W for your suggestion. This is what I’ve got now:

var the_maps_bounds;

function map_maker_js( args ) {
     var map = new google.maps.Map(document.getElementById( args['id'] ), myOptions);
    //rest of the code for building the map goes here

    google.maps.event.addListener(map, 'tilesloaded', viewport_bounds);

    function viewport_bounds() {
        var bounds = map.getBounds();
        var ne = bounds.getNorthEast();
        var sw = bounds.getSouthWest();

        var maxLat = ne.lat();
        var maxLong = ne.lng();
        var minLat = sw.lat();
        var minLong = sw.lng();

        the_maps_bounds = [maxLat, maxLong, minLat, minLong];

        alert(the_maps_bounds); //is the the_map_bounds working

        return(the_maps_bounds);

     }

    }

In script (b), I’ve got:

jQuery.noConflict();

jQuery(document).ready(function($) { //how jQuery scripts are written in WordPress

 alert(window.the_maps_bounds);

//rest of my functions

});

When I view the page, I get “undefined” in the alert box and then, an alert box with the lat longs. I also tried the alert without ‘window’. And on the ready event:

$('#map').ready(function() {
     alert(the_maps_bounds);
 });

I guess that the_maps_bounds in script (b) doesn’t have a value for soem reason. Is the alert firing before its value is set in script (a)? Is something else going on? Any ideas? Thank you.

  • 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-25T22:15:47+00:00Added an answer on May 25, 2026 at 10:15 pm

    Remove var before your the_maps_bound declarations, and use this code:

    var the_maps_bound;
    function map_maker_js( args ) {
     var map = new google.maps.Map(document.getElementById( args['id'] ), myOptions);
    //rest of the code for building the map goes
    ....
    

    This way, you define the_maps_bound outside the scope of these functions. If your functions are defined in the global scope (window), the the_maps_bound variable will also be accessible through window.the_maps_bound. Note: the var declaration has to be in the closest shared parent scope in order to get the code work (see below for a more detailled explanation).

    It’s a good practice to use var, to prevent leaking to the global scope. When you’ve already defined a variable in a parent scope, omitting var inside the local scope will not leak to the global scope. Instead, the closest variable (ie the closest scope where the variable is defined) will be used. If there’s not such a variable, the global scope will be used to store the variable.

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

Sidebar

Related Questions

I need some help understanding this bit of code pre { white-space: pre; white-space:
I need help understanding some C++ operator overload statements. The class is declared like
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help understanding why my concat() is failing and how to fix it.
I need some help understanding some of the points from Paul Graham’s What Made
I need some help understanding what's happening here. This code is from a models/log.py
I need some help understanding the correct way to mix variables with strings like
I need some help understanding how stdext::hash_multimap's lower_bound, upper_bound and equal_range work (at least
I'm new to ORM stuff and I need some help understanding something. Let's assume
I need some help in understanding what the Eval bit does (Just started learning

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.