【问题标题】:Object is not a constructor JavaScript error [duplicate]对象不是构造函数JavaScript错误[重复]
【发布时间】:2017-01-03 19:16:33
【问题描述】:

我正在尝试将一些 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();

是否应该将以下每个方法都定义为原型?

感谢您的帮助!

代码:

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

    }

【问题讨论】:

  • var GeoLocMap = new GeoLocMap();?两个名字一样吗?
  • 问题是您在该行重新分配GeoLocMap 的值。所以函数GeoLocMap 将不再工作,而是作为那个实例,你需要使用不同的变量名。

标签: javascript oop web


【解决方案1】:

当你这样做时

var GeoLocMap = new GeoLocMap();

你真的会

var GeoLocMap = undefined; // hoisted to the top of the scope
// other code in the same scope
GeoLocMap = new GeoLocMap();

因此你的错误。

只需使用不同的名称,例如

var geoLocMap = new GeoLocMap();

更多信息the MDN on variable scope and hoisting

【讨论】:

  • 现在完美运行,非常感谢!
【解决方案2】:

您应该了解JavaScript Hoisting @Denys Séguret 刚刚描述的正在发生的事情。

您应该将新实例分配给不同的变量

var GeoLocMapInstance = new GeoLocMap();

【讨论】:

  • 是否有必要回答说我的回答是正确的?
  • 您的回答没有提供为什么会发生这种情况的上下文,我的只是添加了吊装定义,您可以轻松更新您的,我将删除我的:),因为了解事情发生的原因更多重要的是只说复制/粘贴代码。
  • 大声反对,事实上您也没有指出解决方案,这使您的答案错误,无需对我的答案投反对票
猜你喜欢
  • 2018-06-12
  • 2012-12-22
  • 2017-02-19
  • 2018-02-27
  • 2018-01-18
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
相关资源
最近更新 更多