Prototype in javascript file loaded by requirejs

When I put this code in a script.js file and include it, it works fine,

but when I implement this code in a javascript file loaded by requirejs, the createMapOnOverlay function is not found, which is called from the outside, like this:

var overlay = new AlarmOverlay(...); overlay.createMapOnOverlay(..); 

alarmoverlay.js:

 AlarmOverlay.prototype = new google.maps.OverlayView(); /* constructor */ function AlarmOverlay(bounds, alarmNumber, alarmCssClass) { // initialize all properties for an alarm this.bounds = bounds; this.alarmNumber = alarmNumber; this.alarmCssClass = alarmCssClass; } AlarmOverlay.prototype.createMapOnOverlay = function(map) { // Explicitly call setMap on this overlay this.map = map; this.setMap(map); }; AlarmOverlay.prototype.onAdd = function () { }; AlarmOverlay.prototype.draw = function () { }; 

I have to put the above code in this script.js file below that requirejs loads: but the code below does not work

 define(function() { return function AlarmOverlay(bounds, alarmNumber, alarmCssClass) { var self = this; self.prototype = new google.maps.OverlayView(); self.bounds = bounds; self.alarmNumber = alarmNumber; self.alarmCssClass = alarmCssClass; //AlarmOverlay.prototype.createMapOnOverlay = function(map) { self.map = map; self.setMap(map); //}; AlarmOverlay.prototype.onAdd = function() { }; AlarmOverlay.prototype.draw = function() { }; }; }); 

How can I extract from Google OverlayView that I can call the createMapOnOverlay function from outside, which should call setMap from the base class?

+4
source share
1 answer

in AlarmOverlay.js:

 define(['google'], function(google) { AlarmOverlay.prototype = new google.maps.OverlayView(); /* constructor */ function AlarmOverlay(bounds, alarmNumber, alarmCssClass) { // initialize all properties for an alarm this.bounds = bounds; this.alarmNumber = alarmNumber; this.alarmCssClass = alarmCssClass; } AlarmOverlay.prototype.createMapOnOverlay = function(map) { // Explicitly call setMap on this overlay this.map = map; this.setMap(map); }; AlarmOverlay.prototype.onAdd = function () { }; AlarmOverlay.prototype.draw = function () { }; return AlarmOverlay; } 

and in the js main file:

 require(['AlarmOverlay'], function(AlarmOverlay) { var overlay = new AlarmOverlay(...); overlay.createMapOnOverlay(..); } 
+6
source

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


All Articles