假设我们有一本书的三章,他们都位于自己的URL是这样的:OO的Javascript - 在Object.prototype中的对象实例化
- 第1章= /1.html
- 第2章= /2.html
- 第3章= /3.html
现在假设我们要思考OO,创建2个JS对象(使用jQuery的帮助下):
- 章:它加载到章的元素,
- 预订:垂直地显示的章节(一前一后)。
JS代码:
// Chapter
function Chapter(chapterId)
{
this.chapterId = chapterId;
}
Chapter.prototype =
{
getChapterId: function()
{
var chapterId = this.chapterId;
return chapterId;
},
loadChapter: function(el)
{
$(el).load(this.getChapterId + ".html"); // Ajax
}
}
// Book
function Book()
{
// ?
}
Book.prototype =
{
// ?
}
在您看来,思维面向对象,什么来定义对象“书”,在它的原型方法一起的最佳方式?
什么是最优雅的方式来处理Book.prototype中的对象“章节”的实例化?
谢谢
谢谢@elclanrs。对于不同的答案,我有点困惑。你有没有机会写出更完整的工作示例?在Book对象定义(包括循环/循环)中实例化“章节”类型的对象的最佳方法是什么? –