类构造函数被多次调用

时间:2019-01-25 14:38:14

标签: javascript

我有自己的Google Maps类,该类处理诸如计算距离之类的一些事情。其代码如下:

class Map{
    constructor(options){
        this.mapcenter = [options.lat, options.lng];
    }
    ...other functions
}

在我的主要JavaScript文件中,我在onload之后将其称为:

 $(window).on("load", function() {
    if($("#map").length > 0) {
        themap = new Map({
            lat : 46,
            lng : 7
        });
    }
});

它已经工作了许多个月,但是今天我遇到了一个错误cannot read property lat of undefined,这很奇怪。

显然,一次调用onload会调用构造函数3次,第二次和第三次调用都没有调用定义中的选项。

我找不到类似的东西,我相信它与Google Maps API没有关系,而是我所缺少的逻辑。

1 个答案:

答案 0 :(得分:1)

您应该为您的班级使用一个diffrenet名称,已经有内置的new Map()

相关问题