无法在我的自定义类(Javascript)中继承google.maps.Map V3

Cannot inherit google.maps.Map V3 in my custom class (Javascript)

本文关键字:继承 google maps V3 Map Javascript 我的 自定义      更新时间:2023-09-26

我正在尝试创建一个自定义javascript类,该类继承自google.maps.Map(V3neneneba API)。这是代码:

function MyClass(domElem, options){
    google.maps.Map.call(this, domElem, options);
    var custom_var = new google.maps.MVCArray();
    this.getCustomVar = function(){ return custom_var };
}
MyClass.prototype = new google.maps.Map;
MyClass.prototype.constructor = MyClass;

此代码引发控制台错误,如:

Uncaught TypeError: Cannot read property 'offsetWidth' of undefined

我已经尝试过将其延迟5秒放入setTimeout中,但我认为DOM元素不是问题所在(因为当我使用纯google.maps.Map contractor时,它也能工作)。

谢谢。。。

以下是在javascript:中继承"classes"的正确方法

goog.inherits = function(childCtor, parentCtor) {
   /** @constructor */
   function tempCtor() {};
   tempCtor.prototype = parentCtor.prototype;
   childCtor.superClass_ = parentCtor.prototype;
   childCtor.prototype = new tempCtor();
   childCtor.prototype.constructor = childCtor;
};

所以你可以试试这样的东西:

function MyClass(domElem, options){
   google.maps.Map.call(this, domElem, options);
   var custom_var = new google.maps.MVCArray();
   this.getCustomVar = function(){ return custom_var };
}
goog.inherits(MyClass, google.maps.Map);