简体   繁体   English

OO Javascript-Object.prototype中的对象实例化

[英]OO Javascript - Objects instantiation in Object.prototype

Suppose we have 3 chapters of a book and that they are located on their own URL like this: 假设我们有一本书的三章,它们位于自己的URL上,如下所示:

  • Chapter 1 = /1.html 第1章= /1.html
  • Chapter 2 = /2.html 第2章= /2.html
  • Chapter 3 = /3.html 第3章= /3.html

Now suppose we want to think OO and create 2 JS objects (with the help of jQuery): 现在假设我们要考虑面向对象,并创建2个JS对象(借助jQuery):

  • Chapter : which loads the chapter into an element, and 章节 :将章节加载到元素中,以及
  • Book : which vertically displays the chapters (one after the other). Book :垂直显示章节(一个接一个)。

JS code: 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 =
{
    // ?
}

In your opinion, thinking object-oriented, what's the best way to define the object "Book", along with methods in its prototype? 您认为以面向对象的方式定义对象“ Book”及其原型中的方法的最佳方法是什么?

What's the most elegant way to handle the instantiation of the objects "Chapter" in Book.prototype? 在Book.prototype中处理对象“章节”的实例化的最优雅方法是什么?

Thank you 谢谢

When I started writing OO styled JavaScript I read this article . 当我开始写面向对象的JavaScript时,我读了这篇文章 Some good tips and solutions in there! 那里有一些很好的技巧和解决方案!

I would just pass the chapter ids as a parameter in Book and load the chapters in an array. 我只是将章节ID作为参数传递给Book然后将这些章节加载到数组中。 Something like this: 像这样:

// Book
function Book(chapters) {
  this.chapters = chapters.map(function(id){ return new Chapter(id) });
}

var book = new Book([1,2,3,4]);

Then you can create methods to loop the chapters and manipulate them as needed. 然后,您可以创建方法来循环章节并根据需要进行操作。

have you tried this : 你试过这个吗:

(function (namespace, chapterId) {

var Chapter = function (chapterId) {
    this.chapterId = chapterId;
}

Chapter.prototype ={
getChapterId: function () {
    var chapterId = this.chapterId;
    return chapterId;
},

loadChapter: function (el) {
    $(el).load(this.getChapterId + ".html"); // Ajax
}}

namespace.Chapter = Chapter;
})(new Book(), chapterId);

Ah I didn't read your question well before. 啊,我之前不太清楚您的问题。 I would do something like this: 我会做这样的事情:

// Chapter
var Chapter = function(chapterId) {
    this.chapterId = chapterId;
}

Chapter.prototype.loadChapter: function(el) {
    $(el).load( this.chapterId + ".html" ); // Ajax
}


// Book
var Book = function(chapters) {
    this.chapters = (chapters) ? chapters : [];
    this.numberOfChapters = (chapters) ? chapters : 0; 
    // assume that this has to make sence, so if it is number of chapters, 
    // it start with 0.
}

Book.prototype.addChapter = function () {
    this.chapters.push(new Chapter(++this.numberOfChapters));
}

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

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