requirejs加载的javascript文件中的原型函数
当我将此代码放入script.js文件并包含它时,运行良好 但当我在requirejs加载的javascript文件中实现此代码时,找不到createMapOnOverlay函数,该函数从外部调用如下:requirejs加载的javascript文件中的原型函数,javascript,requirejs,Javascript,Requirejs,当我将此代码放入script.js文件并包含它时,运行良好 但当我在requirejs加载的javascript文件中实现此代码时,找不到createMapOnOverlay函数,该函数从外部调用如下: var overlay = new AlarmOverlay(...); overlay.createMapOnOverlay(..); alarmoverlay.js: AlarmOverlay.prototype = new google.maps.OverlayView(); /* c
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 () {
};
我必须将上述代码放在下面的script.js文件中,该文件由requirejs加载:
但是下面的代码不起作用
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() {
};
};
});
如何从google OverlayView中派生出可以从外部调用createMapOnOverlay函数的函数,该函数应该从基类调用setMap?在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;
}
在主js文件中:
require(['AlarmOverlay'], function(AlarmOverlay) {
var overlay = new AlarmOverlay(...);
overlay.createMapOnOverlay(..);
}