In this code getCurrentPosition method return geoposition object as a parameter if you passed function in that method.
It doesn't return the information as a parameter. It calls the function you give it with the information as an argument (we use the word "argument" in JavaScript, not parameter). Functions are first-class objects in JavaScript, you can pass references to them into functions and use them within the functions.
So if you have a foo
function and want it to call a callback with its result:
function foo(callback) {
// ...come up with the result, then:
callback(result);
}
This is particularly useful for functions that will either A) Call the callback repeatedly (like Array#sort
does), or B) Cll the callback asynchronously (like geolocation
does).
Example:
function giveMeARandom(min, max, callback) {
callback(min + Math.floor(Math.random() * (max - min)));
}
giveMeARandom(1, 10, function(value) {
snippet.log("Value is " + value);
});
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>