gpt4 book ai didi

javascript - 由 requirejs 加载的 javascript 文件中的原型(prototype)函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:59 25 4
gpt4 key购买 nike

当我将此代码放入 script.js 文件并包含它时,它运行正常,

但是当我在 requirejs 加载的 javascript 文件中实现此代码时,找不到从外部调用的 createMapOnOverlay 函数,如下所示:

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 () {


};

我必须将上面的代码放在下面这个由 requirejs 加载的 script.js 文件中:但是下面的代码不起作用

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

关于javascript - 由 requirejs 加载的 javascript 文件中的原型(prototype)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16590161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com