Javascript 如何从对象方法中的回调函数访问对象属性?
我定义这个类:Javascript 如何从对象方法中的回调函数访问对象属性?,javascript,Javascript,我定义这个类: function GMap(map, marker, geocoder) { this.map = map; this.marker = marker; this.geocoder = geocoder; this.setMarker = function(address) { this.geocoder.geocode({'address' : address}, function(results, status) {
function GMap(map, marker, geocoder) {
this.map = map;
this.marker = marker;
this.geocoder = geocoder;
this.setMarker = function(address) {
this.geocoder.geocode({'address' : address}, function(results, status) {
map.setCenter(results[0].geometry.location);
marker.setPosition(results[0].geometry.location);
});
}
}
如何从回调函数访问GMap的映射
和标记
属性
非常感谢。我猜是谷歌地图吧?你为什么要穿过地图和标记?使它们成为全局变量(即:将
var映射;
置于所有函数之外),然后您应该能够从任何地方访问它们
在函数中重用变量名也是个坏主意。如果您首先将它们传递到函数中,那么它们将成为函数变量,因此在函数中定义map、marker和geocoder是毫无意义的,因为您已经能够使用map、marker和geocoder访问它们了。:) 我猜是谷歌地图?你为什么要穿过地图和标记?使它们成为全局变量(即:将
var映射;
置于所有函数之外),然后您应该能够从任何地方访问它们
在函数中重用变量名也是个坏主意。如果您首先将它们传递到函数中,那么它们将成为函数变量,因此在函数中定义map、marker和geocoder是毫无意义的,因为您已经能够使用map、marker和geocoder访问它们了。:) 函数对象原型有一个“apply”方法,可用于在函数中设置“this”的上下文。检查API/代码以了解geocoder.code是什么,许多库将通过一个额外参数为您处理此问题,即:
this.someObj.someFn(params, callback, scope);
在someFn中,它将使用与以下类似的回调:
callback.apply(scope || window, [callbackArg1, callbackArg2]);
这将使“回调”中的“This”上下文成为作为“scope”传入的内容,或者如果没有传入任何内容,“This”将成为窗口的全局上下文。一些javascript库还提供了一种创建回调函数委托的方法,以确保无论从何处调用函数,都始终使用预期的范围调用该函数。这方面的一个例子是
如果您正在使用的库不提供此内置功能,则可以在回调闭包中创建一个要引用的本地变量,即:
this.setMarker = function(address) {
var thisGMap = this;
this.geocoder.geocode({'address' : address}, function(results, status) {
thisGMap.map.setCenter(results[0].geometry.location);
thisGMap.marker.setPosition(results[0].geometry.location);
});
}
函数对象原型有一个“apply”方法,可用于在函数中设置“this”的上下文。检查API/代码以了解geocoder.code是什么,许多库将通过一个额外参数为您处理此问题,即:
this.someObj.someFn(params, callback, scope);
在someFn中,它将使用与以下类似的回调:
callback.apply(scope || window, [callbackArg1, callbackArg2]);
这将使“回调”中的“This”上下文成为作为“scope”传入的内容,或者如果没有传入任何内容,“This”将成为窗口的全局上下文。一些javascript库还提供了一种创建回调函数委托的方法,以确保无论从何处调用函数,都始终使用预期的范围调用该函数。这方面的一个例子是
如果您正在使用的库不提供此内置功能,则可以在回调闭包中创建一个要引用的本地变量,即:
this.setMarker = function(address) {
var thisGMap = this;
this.geocoder.geocode({'address' : address}, function(results, status) {
thisGMap.map.setCenter(results[0].geometry.location);
thisGMap.marker.setPosition(results[0].geometry.location);
});
}
这就是你要找的吗
function GMap(map, marker, geocoder) {
this.map = map;
this.marker = marker;
this.geocoder = geocoder;
var currentGMap = this; // private variable bound to GMap constructor scope
this.setMarker = function(address) {
this.geocoder.geocode({'address' : address}, function(results, status) {
// currentGMap is available (yay closures)
currentGMap.map.setCenter(results[0].geometry.location);
currentGMap.marker.setPosition(results[0].geometry.location);
});
}
}
注意:map和marker也通过闭包绑定,不过我假设您希望在创建GMap实例后能够更改map和marker属性
编辑:是的,我看到凯文在他的awnser的最后一部分也在我面前写了这个。这是你要找的吗
function GMap(map, marker, geocoder) {
this.map = map;
this.marker = marker;
this.geocoder = geocoder;
var currentGMap = this; // private variable bound to GMap constructor scope
this.setMarker = function(address) {
this.geocoder.geocode({'address' : address}, function(results, status) {
// currentGMap is available (yay closures)
currentGMap.map.setCenter(results[0].geometry.location);
currentGMap.marker.setPosition(results[0].geometry.location);
});
}
}
注意:map和marker也通过闭包绑定,不过我假设您希望在创建GMap实例后能够更改map和marker属性
编辑:是的,我看到Kevin在他的awnser的最后一部分中也在我前面提到了这一点。如果您使用的是jQuery,那么有一个名为
$.proxy()
的方法可以用于更改上下文(将函数的“this”设置为您想要的任何值)
如果您使用的是jQuery,那么有一个名为
$.proxy()
的方法可以用于更改上下文(将函数的“this”设置为您想要的任何值)
我正在传递映射和标记以进行依赖项注入=>全局变量在“可测试代码”中被禁止:)我正在传递映射和标记以进行依赖项注入=>全局变量在“可测试代码”中被禁止:)