簡體   English   中英

無法取得屬性或方法

[英]Cannot reach for properties or methods

我很難理解為什么我的變量未定義。

index.html

<!DOCTYPE html>
<html>
 <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/style.css">
    <title>webPage</title>
 </head>
 <body>
    <canvas id="myCanvas"></canvas>
    <script src="JS/const.js"></script>
    <script src="JS/event.js"></script>
    <script src="JS/Player.js"></script>
    <script src="JS/Map.js"></script>
    <script src="JS/App.js"></script>
 </body>
</html>

因此,我假設瀏覽器先讀取Player文件,然后讀取Map文件和App文件(如果我錯了,請更正我)。

App.js

var app;

var App = function (){
    this.map = new Map();
    this.player = new Player();
};


(function (){
    app = new App();
})();

但是,當我想獲取app.map.msg的Map類的屬性或方法時,firebug會拋出typeError並說未定義應用...。此外,typeError表示變量確實存在,但您嘗試執行的操作不適用於其中包含的值。

因此,這意味着我的瀏覽器承認我的變量,但不是我的App類?

我真的很困惑,所以任何幫助將不勝感激!

確保正確定義了地圖。

這是在小提琴上測試的工作示例。

var app;
function Map() {
    this.msg = " hey, i am map" ;
}

function Player () {
    this.msg = " hey, i am player" ;
}

var App = function (){
   this.map = new Map();
   this.player = new Player();
};


(function (){
    app = new App();
    document.getElementById ( 'output' ) .innerHTML = app.map.msg
 })();

http://jsfiddle.net/bdsduv08/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM