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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:04:38+00:00 2026-05-31T07:04:38+00:00

I have the JS scope mixed up. I am trying to assign to values

  • 0

I have the JS scope mixed up. I am trying to assign to values to coordinates and use them later, but for some reason i always get the coordinates as null.

   var thing = (function($){
  var obj = function(config) {
    $.extend(obj.config, config);
    obj.init();
  };
  $.extend(obj, {
      coordinates: {},
         browser_geolocation: function() {
             if (navigator.geolocation) {
                  var timeoutVal = 10 * 1000 * 1000;
                  navigator.geolocation.getCurrentPosition(
                  obj.browser_coordinates, //set coordinates
                    maps.browser_error,
                    { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
                  );
                }
                else {
                  alert("Geolocation is not supported by this browser");
                }
         },

         browser_coordinates: function(position) {
             obj.coordinates.long = position.coords.longitude;
             obj.coordinates.lat = position.coords.latitude;
         },
    });



 $(function() {
        maps.browser_geolocation();
            maps.browser_geolocation();

        console.log(obj.coordinates);
  });
  return obj;
}(jQuery));

I cant seem to figure out what I am doing wrong?

  • 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-31T07:04:40+00:00Added an answer on May 31, 2026 at 7:04 am

    You have two main problems here:

    1. navigator.geolocation.getCurrentPosition is asynchronous.
    2. Undefined (or not included in your example) variables.

    You might want to look into prototypal inheritance in JavaScript, its not as hard as it looks.

    Here is an example of how you could create a customizable object that utilizes callbacks:

    function GetCoordinates(callback) {
    
        var successCallback = function (position) {
            this.coordinates = position.coords;
            callback();
        };
    
        var errorCallback = function (error) {
            throw error;
        };
    
        var self = this;
        if (navigator.geolocation) {
            var timeoutVal = 10 * 1000 * 1000;
            navigator.geolocation.getCurrentPosition(function () {
                successCallback.apply(self, arguments)
            }, errorCallback, {
                enableHighAccuracy: true,
                timeout: timeoutVal,
                maximumAge: 0
            });
        }
    
    }
    
    
    (function() {
    
        // callback to execute once coordinates are ready
        var myCallback = function() {
            console.log(c.coordinates); // Coordinates
        };
    
        // create object (don't forget new :))
        var c = new GetCoordinates(myCallback);
    
        // because geolocation.getCurrentPosition is async, the following will be undefined
        console.log(typeof c.coordinates);
    
    }());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Javascript functions are objects and they have scope chain associated with them. If some
The scope of this is that we have three main projects. Some of the
I have: scope :all_somethings, lambda { find(:all) } But this returns an array (and
I have been trying to get a grasp on how implicit parameters work in
I've never used nested functions, but have seen references to them in several languages
If I have a scope with a lambda and it takes an argument, depending
I have the following scope, which I know is not optimal: scope :event_stream_for, lambda{
What is the scope of variables in javascript? Do they have the same scope
I have the following named_scope which works fine in MySQL and sqlite but bombs
I have the following named scope: named_scope :report_search, lambda { |search| { :conditions =>

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.