我有 2 个函数,想从另一个函数调用一个函数并获取 2 个值。
renderMap: function(){
// here I need to call getCurrentLocation function and get 2 values - lat and lng from it.
console.log(lat, lng);
},
getCurrentLocation: function() {
var me = this,
lat,
lng;
Ext.device.Geolocation.getCurrentPosition({
success: function(position) {
me.lat = position.coords.latitude;
me.lng = position.coords.longitude;
}
});
},
请告知如何正确执行此操作。
最佳答案
当您调用 Ext.device.Geolocation.getCurrentPosition
时,
me
不再存在。
此时您已处于不同的上下文或范围中,Ext.device.Geolocation
类对 me
变量一无所知。
在方法的开头,它引用了您的 Controller ,但随后您调用了 Ext.device.Geolocation
类的 getCurrentPosition
方法,因为您没有传递任何方法 me
的参数应为 undefined
。
我认为你可以用 Ext.bind 解决这个问题
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext-method-bind
同时在 JavaScript 中查找 call 和 apply :)
最诚挚的问候@code4jhon
关于javascript - Sencha Touch - 无法从其他函数获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23447491/