4
所以,我想扩展一个谷歌地图类,特别是google.maps.OverlayView(在v3中)。这样做香草js方式完全有效。原型Class.create没有正确的子类
POIOverlay = function(marker, poi, type)
{
this._marker = marker;
this._poi = poi;
this._type = type;
this._div = null;
this.latlng_ = marker.getPosition();
this._map = marker.getMap();
this._offsetVertical = -195;
this._offsetHorizontal = 0;
this._height = 165;
this._width = 266;
}
POIOverlay.prototype = new google.maps.OverlayView();
POIOverlay.prototype.create = function()
{
console.log(this)
}
POIOverlay.prototype.draw = function()
{
//stuff
}
然而,这样做原型的方式,无法添加的父类方法的任何:
POIOverlay = Class.create(new google.maps.OverlayView(), {
initialize : function(marker, poi, type)
{
this._marker = marker;
this._poi = poi;
this._type = type;
this._div = null;
this.latlng_ = marker.getPosition();
this._map = marker.getMap();
this._offsetVertical = -195;
this._offsetHorizontal = 0;
this._height = 165;
this._width = 266;
},
create : function()
{
if(this._div) return;
console.log(this);
},
draw : function()
{
//stuff
}
});
这里是实例/使用类的代码:
try
{
poio = new POIOverlay(marker,poi,type);
}
catch(e)
{
console.log(e);
}
google.maps.event.addListener(marker, 'click',
poio.draw.bind(poio)
);
在第一个示例中,控制台使用父级和子级方法/属性记录对象。在第二个示例中,控制台记录一个没有父属性/方法的对象。
显然,这是不是太什么大不了的事,但我在任何人想知道其他人碰到这个问题,如果它是很容易纠正。我正在使用原型1.7。
不知父类必须是一个由原型自己的系统中创建... – Pointy 2011-03-04 20:11:57