【问题标题】:OO Javascript - Objects instantiation in Object.prototypeOO Javascript - Object.prototype 中的对象实例化
【发布时间】:2023-03-05 22:53:01
【问题描述】:

假设我们有一本书的 3 个章节,并且它们位于各自的 URL 上,如下所示:

  • 第 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”及其原型中的方法的最佳方式是什么?

在 Book.prototype 中处理对象“Chapter”实例化的最优雅的方式是什么?

谢谢

【问题讨论】:

    标签: javascript jquery oop design-patterns prototype


    【解决方案1】:

    当我开始编写面向对象风格的 JavaScript 时,我阅读了 this article。里面有一些很好的提示和解决方案!

    【讨论】:

      【解决方案2】:

      我只需将章节 ID 作为参数传递到 Book 中,然后将章节加载到数组中。像这样的:

      // Book
      function Book(chapters) {
        this.chapters = chapters.map(function(id){ return new Chapter(id) });
      }
      
      var book = new Book([1,2,3,4]);
      

      然后您可以创建方法来循环章节并根据需要对其进行操作。

      【讨论】:

      • 谢谢@elclanrs。我对不同的答案有点困惑。你有机会写一个更完整的工作示例吗?将“Chapter”类型的对象实例化到 Book 对象定义(包括循环/循环)中的最佳方法是什么?
      【解决方案3】:

      你试过了吗:

      (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);
      

      【讨论】:

      • 方法是什么?这是我写的内容的复制/粘贴。抱歉,它没有回答问题。
      • 没问题,但还是不明白你的答案。 Book 对象定义在哪里(应该实例化一些“Chapter”类型的对象)?
      【解决方案4】:

      啊,我之前没有很好地阅读您的问题。我会做这样的事情:

      // 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));
      }
      

      【讨论】:

      • 关于我使用this.getChapterId的原因:stackoverflow.com/questions/17173289/…
      • 我同意“保持简单”但是 - 名字?作者?谁提到了他们?我只是在问如何定义一个应该使用 Chapter 对象的 Book 对象。谢谢。
      • 关于 this.getChapterId 我还是不同意。解释不正确!观看如何制作私有变量,然后您就可以使用它们了。 youtube.com/…
      • 感谢您的视频。我错了还是在您的编辑中 Book 对象尚未使用 Chapter 对象? “新”运算符在哪里?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2016-05-15
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      相关资源
      最近更新 更多