gpt4 book ai didi

javascript - 对象不是构造函数 JavaScript 错误

转载 作者:行者123 更新时间:2023-11-30 07:55:21 25 4
gpt4 key购买 nike

<分区>

我正在尝试将一些 OO 设计模式应用于现有脚本,并且在按照 Mozilla 帮助中心的一些指南进行操作后,我在尝试实例化对象时仍然遇到错误。

我查看了这些资源以寻求帮助,但我可能没有完全理解 JavaScript 语法。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

错误:

Uncaught TypeError: GeoLocMap is not a constructor

错误行:

var GeoLocMap = new GeoLocMap();

下面的每个方法都应该定义为原型(prototype)吗?

感谢您的帮助!

代码:

function GeoLocMap() {

this.map = -1;
this.regionName = "";
this.options = -1;
this.openCountData = -1;
this.chart = -1;

this.getMap = function () {
if (this.map == -1) {
this.map = new google.maps.Map(document.getElementById('geochart-colors'), {
zoom: 6,
//TODO replace with the region retrieved from eventData
center: new google.maps.LatLng(lat, long),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
} else {
return this.map;
}
};

this.getMapWidth = function () {
var docBody = document.body;
var docElem = document.documentElement;

if (typeof document.width !== 'undefined') {
return document.width;// For webkit browsers
} else {
return Math.max(docBody.scrollWidth, docBody.offsetWidth, docElem.clientWidth, docElem.scrollWidth, docElem.offsetWidth);
}
};

this.getMapHeight = function () {
var docBody = document.body;
var docElem = document.documentElement;

if (typeof document.height !== 'undefined') {
return document.height;// For webkit browsers
} else {
return Math.max(docBody.scrollHeight, docBody.offsetHeight, docElem.clientHeight, docElem.scrollHeight, docElem.offsetHeight);
}

};

this.getChart = function() {

if (this.chart == -1){
this.chart = new google.visualization.GeoChart(document.getElementById('geochart-colors'));
}

return this.chart;

};

this.getOpenCountData = function () {

if (this.openCountData == -1) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Country');
data.addColumn('number', 'Open Count');

this.openCountData = data;

}

return this.openCountData;
};

this.addOpenCountDataRow = function(dataRow) {

if (this.openCountData == -1){
this.getOpenCountData();
}

if (dataRow == -1){
return -1;
}

this.openCountData.addRows(dataRow);

return 1;

};

this.getOptions = function () {

if (this.options == -1) {
this.options = {
width: width,
height: height,
region: 'US',
resolution: 'provinces',
colors: ['#FFFFFF', '#FFFFFF']
};
}

return this.options;
};

this.setOptions = function (property, value) {

if (this.options == -1) {
this.getOptions();
}

if (value === undefined) {
return -1;
}

this.options[property] = value;

return 1;

};

this.drawMap = function() {

if (this.chart == -1){
this.getChart();
}

if (this.options == -1){
this.getOptions();
}

if (this.openCountData == -1){
this.getOpenCountData();
}

this.chart.draw(this.openCountData, this.options);
};

}

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