返回一个回调函数的值? [英] Returning a value of callback function?
本文介绍了返回一个回调函数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用函数回调方面遇到了一些麻烦,特别是HTML5地理定位API。我也是Js的新手。函数:
I'm having some trouble with function callbacks, specifically with the HTML5 geolocation API. I'm a little new to Js as well. The function:
function getInfo()
{
navigator.geolocation.getCurrentPosition(getPos);
}
获取一个函数的回调参数,该函数获取传递给它的位置。
takes a callback parameter of a function that gets the position passed into it.
function getPos(position)
{
PositionObj.lat = position.coords.latitude;
PositionObj.lon = position.coords.longitude;
}
我创建了一个位置对象来存储坐标,我是返回时遇到问题。
I created a "location" object to store the coordinates, and I'm having trouble returning them.
var PositionObj =
{
lat:null,
lon:null,
returnLat: function()
{
return this.lat;
},
returnLon: function()
{
return this.lon;
}
};
function printStuff()
{
getInfo();
console.log(PositionObj.returnLat());
}
编辑:语法错误已修复。当我调用printStuff()时,我仍然得到null。
Syntax mistakes fixed. When I call printStuff(), I still get "null."
推荐答案
- 要传递回调,只需使用
getPos
而不是立即调用该函数。 - 使用正确的语法在
PositionObj
中声明属性。 - To pass the callback, use just
getPos
instead of calling the function right away. - Use proper syntax to declare properties in
PositionObj
.
PositionObj = {
lat: null,
lon: null,
}
这篇关于返回一个回调函数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文