我是JavaScript新手,无法从外部访问对象。
我的方法用对象填充数组,然后再访问该对象的属性。
Reservation = {
objArr: [], // stores CalendarDates objects
dates: ["test", "foo", "bar"], // some test strings
actionMethod: function() {
Reservation.objArr[0] = new Reservation.CalendarDate.construct(Reservation.dates[0]);
alert(Reservation.dates[0].testVariable); // returns 'undefined'
}
};
Reservation.CalendarDate = {
testVariable: '',
construct: function(testVariable) {
Reservation.CalendarDate.testVariable = testVariable;
}
};
我如何从存储在数组中的对象外部访问属性(testVariable)。
谢谢你的帮助!
最佳答案
我认为您正在尝试按照这些方针完成工作...
var Reservation = {
objArr: [], // stores CalendarDates objects
dates: ["test", "foo", "bar"], // some test strings
actionMethod: function() {
var temp = new Reservation.CalendarDate(Reservation.dates[0]);
Reservation.objArr[0] = temp;
alert(Reservation.objArr[0].testVariable);
}
};
Reservation.CalendarDate = function(testVariable) {
this.testVariable = testVariable;
};
Reservation.actionMethod();