Google Geocoder 和 TypeScript 中的“this”引用
Google Geocoder and 'this' reference in TypeScript
调用地理编码器后,我在尝试获取对主对象的引用时遇到问题。
export class Oferta{
_self: any;
geocoder: any;
contructor (){
this._self = this;
this.geocoder = null;
}
geolocalizate(){
var _this = this;
var address = "Home";
this.geocoder = new google.maps.Geocoder();
this.geocoder.geocode({
'address': address
}, function(results,status){
// 'this' here is a reference to 'window'
// _self and _this are 'undefined'
});
}
}
我无法找到解决此问题的方法
> 只需使用箭头函数=>
而不是function
。也不要做_this
因为打字稿保留了用于词汇范围。完成:
export class Oferta{
_self: any;
geocoder: any;
constructor (){
this._self = this;
this.geocoder = null;
}
geolocalizate = () => { // ARROW
var address = "Home";
this.geocoder = new google.maps.Geocoder();
this.geocoder.geocode({
'address': address
}, (results,status) => { // ARROW
// use `this`
});
}
}
更多 : https://www.youtube.com/watch?v=tvocUcbCupA&hd=1
你也拼错了constructor
,这就解释了为什么_self
错了。
相关文章:
- jQuery$(this)引用未定义
- Javascript对象文字,如何使用“this”来引用对象中的变量
- 为什么当我尝试将函数绑定到自身时,“this”对象仍然引用窗口
- 为什么“exports”在nodejs模块中引用与“this”相同的对象
- React如何调用ES6类的呈现函数,使“this”不引用类本身
- 在 javascript 中将“this”引用传递给方法的提示
- 如何避免“this”引用 DOM 元素,并引用对象
- 主干:在回调中维护对模型的“this”引用的最简单方法
- jQuery Resize 会丢失“this”引用
- 将“this”引用到自定义变量
- 如何使“this”引用成员函数而不是其所有者类的实例
- Google Geocoder 和 TypeScript 中的“this”引用
- Javascript“this”-引用包含对象
- 我以为我有一个this引用传递到Javascript闭包中的私有函数,我想错了
- 我怎样才能确保'this'引用对象文字中的对象文字
- Ajax.BeginForm JavaScript回调参数导致$(this)引用窗口而不是窗体
- 如何防止方法's ' this '引用调用它的对象
- 有没有可能保护'this'引用绑定对象内的函数
- “this”引用在nodeJs中不起作用
- JavaScript 'this' 引用了错误的范围