我正在尝试学习JQuerymobile并且我正在做类似下面的事情,只是想检查JQueryMobile是否支持这样的事情
function BooksCategory () {
ID = '';
title = '';
imgURL = '';
description = '';
bookCounts= '';
Books = [];
};
function Books () {
categoryID = '';
bookID = '';
name = '';
imgURL = '';
Cost = '';
termsandcondition = '';
}
this.booksCategory = [];
ParseResponse()
{
session.CatagoriesCount = response.BooksCategory.length;
for (var k=0; k < session.CatagoriesCount; k++)
{
session.booksCategory[k] = new BookCategory();
var NoOfBooks = session.BooksCategory[k].bookCounts
for (var j=0; j<NoOfBooks; j++)
session.booksCategory[k].Books[j] = new Books();
}
}
为什么我得到“未捕获的TypeError:无法读取”此声明的属性'0'
session.booksCategory[k].Books[j] = new Books();
如果这不是正确的方法,我们如何动态地在数组中创建一个数组?
此致 拉维
答案 0 :(得分:0)
更改
function BooksCategory () {
ID = '';
title = '';
imgURL = '';
description = '';
bookCounts= '';
Books = [];
};
到
function BooksCategory () {
this.ID = '';
this.title = '';
this.imgURL = '';
this.description = '';
this.bookCounts= '';
this.Books = [];
};
将Books属性添加到使用BooksCategory
创建的new
实例中。没有它,session.booksCategory[k].Books
不存在,因此您无法获取其元素[0]
,因此您的错误。
上下文this
并非隐含在javascript中。
当然,对其他班级来说也是如此。
编辑:
并替换
var NoOfBooks = session.BooksCategory[k].bookCounts
通过
var NoOfBooks = session.booksCategory[k].bookCounts;