我试图获取这些变量:
Geoposition.coords.latitude
Geoposition.coords.longitude
顺便提一下,来自 navigator.geolocation.getCurrentPosition
:
function getPosition() {
"use strict";
var coordinates = {
latitude: null,
longitude: null,
error: false,
testData: 'Hello, world!'
}
function success (pos) {
coordinates.latitude = pos.coords.latitude;
coordinates.longitude = pos.coords.longitude;
coordinates.testData = 'I am John';
console.log(coordinates); //here it is ok, latitude,longitude and testData were recorded
}
function fail(error){
coordinates.error = true;
}
navigator.geolocation.getCurrentPosition(success, fail);
return coordinates; // but here it is no ok: function returns "coordinates" in initial state (null, null, false, "Hello, world!")
}
据我所知,在JavaScript中,对象不会被克隆,但在这里似乎被克隆了。我做错了什么?如何在回调函数“success”之外使用coords?