Class.create prototype not properly subclassed

So, I'm trying to expand the google map class, specifically google.maps.OverlayView (in version 3). Executing this vanilla js method fully works.

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 } 

However, making this a prototype, you cannot add any of the methods of the parent class:

 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 } }); 

Here is the code to create / use the class:

  try { poio = new POIOverlay(marker,poi,type); } catch(e) { console.log(e); } google.maps.event.addListener(marker, 'click', poio.draw.bind(poio) ); 

In the first example, the console registers an object with parent and child methods / attributes. In the second example, the console registers an object without parent attributes / methods.

Obviously, this is not a big deal, but I was wondering if anyone else had this problem, and if it is easy to fix. I am using prototype 1.7.

+4
source share
1 answer

The argument of the superclass should be the proper prototype of the "Class" - remember that classes do not actually exist in JavaScript. There are several classic inheritance models for JavaScript, and you should be able to get a prototype chain (including a link to the parent "class" and prototype) by manually creating proxy servers and prototypes.

From prototype class.js :

[[Class.create]] accepts two types of arguments. If the first argument is [[Class]], it is used as the superclass of the new class, and all its methods are inherited. Otherwise, any arguments passed are treated as objects and their methods are copied (β€œmixed”) as methods of the instance of the new class.

+2
source

Source: https://habr.com/ru/post/1342346/


All Articles