I'm trying to apply some OO design patterns to an existing script, and after following some guides on Mozilla's help center, I'm still getting an error when attempting to instantiate the object.
I've looked at these resources for help, but maybe I'm not fully understanding the JavaScript syntax.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
Error:
Uncaught TypeError: GeoLocMap is not a constructor
The line of the error:
var GeoLocMap = new GeoLocMap();
Should each of the methods below be defined as a prototype?
Thanks for the help!
Code:
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);
};
}