Javascript 如何正确调用JSON对象的回调函数
详细信息:Javascript 如何正确调用JSON对象的回调函数,javascript,extjs,Javascript,Extjs,详细信息: Ext.define('Ext.ux.GeoLocation', { alias: 'plugin.ux.geolocation', constructor: function() { console.log('called 1'); this.duh = 'yes'; this.init(); }, init: function () { console.log('called 2
Ext.define('Ext.ux.GeoLocation', {
alias: 'plugin.ux.geolocation',
constructor: function() {
console.log('called 1');
this.duh = 'yes';
this.init();
},
init: function () {
console.log('called 2');
navigator.geolocation.getCurrentPosition(this.test, this.test);
},
test: function(init) {
console.log('called 3');
this.duh = "amazing";
}
});
var geo = new Ext.ux.GeoLocation();
geo.test(); // this gets called twice though... incorrect?
我正在为html5
地理定位获取数据;特别是经度和纬度。我正在尝试创建一个插件,任何人都可以从一个项目中调用该插件,并获得long和lat
我已经读过了,但是我想知道应该如何调用这些函数
我放置了一个extjs
标记,尽管我认为这更像是一个javascript
核心问题
JavaScript:
Ext.define('Ext.ux.GeoLocation', {
alias: 'plugin.ux.geolocation',
constructor: function() {
console.log('called 1');
this.duh = 'yes';
this.init();
},
init: function () {
console.log('called 2');
navigator.geolocation.getCurrentPosition(this.test, this.test);
},
test: function(init) {
console.log('called 3');
this.duh = "amazing";
}
});
var geo = new Ext.ux.GeoLocation();
geo.test(); // this gets called twice though... incorrect?
控制台输出为:
called 1
called 2
called 3
called 3
这种做法不好吗?还有其他解决方案吗?在类构造函数上,您正在将
test
函数传递给navigator.geolocation.getCurrentPosition
。你知道另一个函数做什么吗?最有可能的是在内部调用test
您可以通过执行以下操作进行检查:
var geo = new Ext.ux.GeoLocation();
console.log("middle");
geo.test();
然后,您应该会看到在两行“called 3”之间打印“middle”。那么,我只需调用
geo.test()
?此时,当我到达geo.test()
时,getCurrentPosition()
已经启动了。。。对吗?我不知道extjs,但似乎“init”是在对象构造时被调用的某种“构造函数”。是的,getCurrentPosition()正在对象构造上执行(这就是为什么您还会看到在“调用3”之前打印的“调用2”),我在构造函数中调用this.init()
。构造函数在内部被调用。实际上我删除了init()
,它仍然有效……不,我问错了问题。对于javascript
,我所寻找的是不可能的,不过您的回答让我来回答,所以谢谢:)