나는 script1
과 script2
입니다.자바는 콜백 함수와 실행을 이해합니다.
var pos;
function foo(param){pos = param;}
bar(foo);
script2
에서 :
function bar(callback){
//prints 'function'
console.log(typeof callback);
navigator.geolocation.getCurrentPosition(
success,
fail
);
}
function success(position, callback){
//prints 'undefined'
console.log(typeof callback);
var pos= {
lat: position.coords.latitude,
long: position.coords.longitude
};
callback(pos);
}
어떻게 제대로 bar
에 콜백 함수로 foo
을 통과하고 navigator.geolocation.getCurrentPosition
에 체인 않습니다 여기에 script1
에서 일부 코드는? pos
을 script1
으로 올바르게 다시 가져 오려면 어떻게해야합니까? 대신 값을 반환 할 수없는 이유는 무엇입니까?
에 콜백을 다시 통과해야합니까? –
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition –
'navigator.geolocation.getCurrentPosition'의 비동기 동작으로 인해, 내가하고있는 것에 대한 약속을 사용하는 것이 좋습니다. 오늘이 사용 사례에 대해 제출 한 답변은 다음과 같습니다. http://stackoverflow.com/questions/38287407/cant-return-get-access-to-a-variable-inside-a-function/38287844#38287844 – jmealy