Javascript-对象中未定义的对象
我知道有很多类似的问题,我已经看了很多,但还没有找到我的答案 我创建了一个名为Javascript-对象中未定义的对象,javascript,object,Javascript,Object,我知道有很多类似的问题,我已经看了很多,但还没有找到我的答案 我创建了一个名为目的地的自定义对象: function destination() { var city = ""; var flightNumber = ""; var type = ""; } function plane() { var flightNumber = ""; var otherCity = new destination(); var status = "";
目的地的自定义对象:
function destination() {
var city = "";
var flightNumber = "";
var type = "";
}
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
:
function destination() {
var city = "";
var flightNumber = "";
var type = "";
}
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”
有人能指出我错在哪里吗?它快把我逼疯了
提前谢谢。您有错。您需要而不是带有var声明的局部变量
function Destination() {
this.city = "";
this.flightNumber = "";
this.type = "";
}
顺便说一句,我建议使用带有大写首字母的类声明标准。非常感谢,这么简单的答案。