javascript - 使用 self.var 的动态变量

标签 javascript

我想知道我们是否可以使用“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/

相关文章:

javascript - 删除 node.js 数组中具有相同开头的项

javascript - 输入大小的 Ember 全局变量

javascript - 具有当前文化的 jQuery DatePicker 日期格式

javascript - jQuery live 函数的原型(prototype)等价物

javascript - 在 react 应用程序中无法将对象转换为原始值错误?

javascript - 使用其中的字符串作为函数名称(Javascript)

javascript - 如何从数组中提取子文档?

javascript - 当我尝试使用 settimeout 刷新页面时,它无法正常工作

Javascript 和 HTML5 - 加载 .txt 文件,将行拆分为不同的输入

javascript - 如何将 .jsx 文件中的绝对路径更改为相对路径。 (Photoshop 扩展脚本)?