2017-01-03 10 views
1

既存のスクリプトにいくつかのオブジェクト指向設計パターンを適用しようとしていますが、Mozillaのヘルプセンターのガイドに従って、オブジェクトをインスタンス化しようとするとエラーが発生します。オブジェクトがコンストラクタではありませんJavaScriptエラー

私はこれらのリソースを参考にしましたが、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); 
     }; 

    } 
+1

'VARのGeoLocMap =新しいGeoLocMap()に新しいインスタンスを割り当てる必要がありますか;'?どちらも同じ名前ですか? –

+0

問題は、その行に 'GeoLocMap'の値を再割り当てすることです。だから関数 'GeoLocMap'はもはや動かず、代わりにそのインスタンスになるでしょう。あなたは別の変数名を使う必要があります。 –

答えて

6

あなたは

var GeoLocMap = new GeoLocMap(); 

を行うと、あなたが本当にそれゆえ

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

あなたのエラーを行います。

ちょうどあなたが@DenysSéguretがちょうど述べたものをJavaScript Hoistingを理解しておく必要がありthe MDN on variable scope and hoisting

+0

今、完璧に動作します、ありがとう! – Colby

-1

var geoLocMap = new GeoLocMap(); 

詳細情報、たとえば、異なる名前を使用するには、何が起こっているかです。

あなたは別の変数

var GeoLocMapInstance = new GeoLocMap(); 
+2

私の言うことは正しいと答える必要がありますか? –

+1

あなたの答えはなぜ起こっているのかの文脈を提供するものではありません。ホイスト定義を追加してください。簡単に更新できます。私は削除します:)。なぜ物事が起こるのかを理解することはコピー/ペーストコード。 –

+0

Lol downvotes、あなたも解決策を指摘していなかったという事実はあなたの答えが間違っている必要はありません私の答えを投票する必要はありません –

関連する問題