简体   繁体   English

无法取得属性或方法

[英]Cannot reach for properties or methods

I have difficulties understanding why my variable is undefined. 我很难理解为什么我的变量未定义。

index.html 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>

So i assume that the browser reads the Player file first, then the Map file and the the App file(correct me if i'm wrong). 因此,我假设浏览器先读取Player文件,然后读取Map文件和App文件(如果我错了,请更正我)。

App.js App.js

var app;

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


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

But when i want to reach for a property or a method of the Map class for exemple app.map.msg , firebug throws a typeError and says that app is not defined... Plus, typeError means that the variable does exist but that the operation you're trying to perform is not appropriate for the value it contains. 但是,当我想获取app.map.msg的Map类的属性或方法时,firebug会抛出typeError并说未定义应用...。此外,typeError表示变量确实存在,但您尝试执行的操作不适用于其中包含的值。

So it means my browser acknowledges my variable but not my App class?? 因此,这意味着我的浏览器承认我的变量,但不是我的App类?

I'm really confused so any help will be appreciated!! 我真的很困惑,所以任何帮助将不胜感激!

Make sure you defined map correctly. 确保正确定义了地图。

here is a working example tested on fiddle. 这是在小提琴上测试的工作示例。

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/ http://jsfiddle.net/bdsduv08/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM