0

I am teaching myself javascript and one of the exercises I have tried makes no sense to me.

In the code below 'position' is a parameter that must be defined. However, when I pass the function, displayLocation as an argument to navigator.geolocation.getCurrentPosition, the code executes flawlessly, despite no argument being passed. When I try coords.longitude in the console without the position argument, it does not work.

How does the code work without a defined argument?

function displayLocation(position)
{
  var latitude = latitude.coords.latitude;
  var longitude = position.coords.longitude;
  var div = document.getElementById("location");
  div.innerHTML = latitude + " " + longitude;
}

navigator.geolocation.getCurrentPosition(displayLocation);
dey.shin
  • 990
  • 10
  • 21
  • 1
    Please give an example of the output both for the working and non working version. – Difster Jul 07 '17 at 19:03
  • Here is a link to the working code that was recommended by my text book. https://jsfiddle.net/2o4eLckf/1/#&togetherjs=CMK3uEvhVa – Brian T Brennglass Jul 07 '17 at 19:19
  • 1
    Not to be picky, but the first line in this function should probably read `var latitude = position.coords.latitude;`. –  Jul 07 '17 at 19:21
  • Here is the non-working version: https://jsfiddle.net/rq74mhpj/#&togetherjs=dVAE8GIAPv. I cannot understand how an undefined argument makes such a difference to the code. – Brian T Brennglass Jul 07 '17 at 19:26
  • Usually the [API reference](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) will tell you what parameters are passed into the callback(s) – James Jul 07 '17 at 21:29

4 Answers4

0

When I try coords.longitude in the console without the position argument, it does not work...

How does the code work without a defined argument?

Arguments of function in JavaScript are optional.
If you don't pass it, it has as value : undefined. So, yes the function accepts an invocation with a missing argument position but position.coords would not be usable.

Community
  • 1
  • 1
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

navigator.geolocation.getCurrentPosition(displayLocation); calls your function with an instance of Position, as documented here.

Please take into account that displayLocation is not called immediately but delayed. This is why you pass it as a callback function.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
ideaboxer
  • 3,863
  • 8
  • 43
  • 62
0

What you are doing when you do

navigator.geolocation.getCurrentPosition(displayLocation);

is that you are calling navigator.geolocation.getCurrentPosition, and giving it a handle to your function displayLocation. You cannot say that displayLocation is getting called without an argument. That depends on what getCurrentPosition does with that handle. What it does is that it creates a position object, and then calls the function you gave it, with that object as a parameter. More succinctly, getCurrentPosition calls displayLocation with a position object, it created, as a parameter.

0

Let's take a look at Geolocation.getCurrentPosition. The required parameter success is a callback function.

Inner workings of the Geolocation.getCurrentPosition(displayLocation) will look roughly like the following:

function getCurrentPosition(callback, ...){

  var positionGCP;
  ...
  ...

  positionGCP = *someposition*;
  // done getting current position

  callback(positionGCP) 
  //passed in function gets called back!
}

and the last line (callback(positionGCP)) really runs displayLocation(positionGCP) since callback == displayLocation.

Does that make sense?

The following link may help: Passing parameters to a callback function

dey.shin
  • 990
  • 10
  • 21