我想知道我们是否可以使用" self.variable"
使用动态变量我目前有两个地图。我有一个将各种内容应用于地图的函数。我想在变量中使用当前地图呼叫我的功能时,我想更改映射。
googleMap = Class.extend({
map: null,
miniMap: null,
isOverlayVisible: false,
overlay: null,
});
我的脚本具有以下代码。
self.map.fitBounds(bounds);
self.map
是指类中定义的变量。我也有变量self.minimap
。
我如何将"映射"内部变量(如下
function updateMap(currentMap){
if(typeof currentMap !== "undefined"){
currentMap = map;
}
var currentMap = 'minimap';
self.currentMap.fitBounds(bounds);
}
updateMap('minimap');
谢谢。
您可以通过两种方式访问对象属性
一个是.
,第二是[variable or string]
self[currentMap].fitBounds(bounds);
看到这个https://developer.mozilla.org/en-us/docs/web/javascript/reference/reference/property_accessors.