Col*_*lby 1 javascript oop web
我正在尝试将一些OO设计模式应用于现有脚本,并且在遵循Mozilla帮助中心的一些指南之后,我在尝试实例化对象时仍然遇到错误.
我已经查看了这些资源以寻求帮助,但也许我并不完全理解JavaScript语法.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
错误:
Uncaught TypeError: GeoLocMap is not a constructor
Run Code Online (Sandbox Code Playgroud)
错误的一行:
var GeoLocMap = new GeoLocMap();
Run Code Online (Sandbox Code Playgroud)
是否应将以下每种方法定义为原型?
谢谢您的帮助!
码:
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);
};
}
Run Code Online (Sandbox Code Playgroud)
当你这样做
var GeoLocMap = new GeoLocMap();
Run Code Online (Sandbox Code Playgroud)
你真的这样做
var GeoLocMap = undefined; // hoisted to the top of the scope
// other code in the same scope
GeoLocMap = new GeoLocMap();
Run Code Online (Sandbox Code Playgroud)
因此你的错误.
例如,只需使用其他名称即可
var geoLocMap = new GeoLocMap();
Run Code Online (Sandbox Code Playgroud)
有关可变范围和提升的MDN中的更多信息
| 归档时间: |
|
| 查看次数: |
331 次 |
| 最近记录: |