【发布时间】:2011-03-04 19:53:08
【问题描述】:
所以,我正在尝试扩展一个谷歌地图类,特别是 google.maps.OverlayView(在 v3 中)。用 vanilla js 的方式来做是完全可行的。
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。
【问题讨论】:
-
我想知道父类是否必须是由Prototype自己的系统创建的......
标签: javascript oop prototype