繁体   English   中英

Javascript - 对象中未定义的对象

[英]Javascript - Undefined object in an object

我知道这个问题有很多类似的问题,我看了很多,但还没找到答案。

我创建了一个名为destination的自定义对象:

function destination() {
    var city = "";
    var flightNumber = "";
    var type = "";
}

然后我创建了第二个自定义对象,该对象的一个​​属性是destination类型:

function plane() {
    var flightNumber = "";
    var otherCity = new destination();
    var status = "";
    var taxiRoute = [];
    var airRoute = "";
    var heading = 0;
    var speed = 0;
    var left = 0;
    var top = 0;
    var height = 0;
    var width = 0;
    var dx = 0;
    var dy = 0;
}

但是,每当我尝试使用类似以下内容访问任何类型destination的属性时:

aPlanes[0].otherCity.city;

其中aPlanes是一个plane()对象数组,我在浏览器控制台中收到未定义的错误消息:

未捕获的TypeError:无法读取未定义的属性“city”

有人能够指出我哪里出错吗? 这让我很生气!

提前致谢。

你有一个错误的类定义 你需要this而不是var声明的局部变量。

function Destination() {
    this.city = "";
    this.flightNumber = "";
    this.type = "";
}

顺便说一下,我建议使用大写首字母的类声明标准。

暂无
暂无

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

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