我想知道我们是否可以使用“self.variable”来使用动态变量
我目前的类(class)有两张 map 。我有一个函数可以将各种东西应用于 map 。我想使用变量中的当前 map 来调用我的函数来动态更改 map 。
googleMap = Class.extend({
map: null,
miniMap: null,
isOverlayVisible: false,
overlay: null,
});
我的脚本包含以下代码。
self.map.fitBounds(bounds);
self.map
指类中定义的变量。我还有变量 self.minimap
.
我怎样才能在变量中包含“map”,如下所示:
function updateMap(currentMap){
if(typeof currentMap !== "undefined"){
currentMap = map;
}
var currentMap = 'minimap';
self.currentMap.fitBounds(bounds);
}
updateMap('minimap');
谢谢。
最佳答案
您可以通过两种方式访问对象属性
第一个是.
,第二个是[变量或字符串]
self[currentMap].fitBounds(bounds);
看这个 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors .
关于javascript - 使用 self.var 的动态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49988689/