在JavaScript中将对象作为模型

时间:2018-09-30 19:28:29

标签: javascript

很抱歉,如果我的问题确实很琐碎,只想澄清一下。 想象我有图书馆课,有书课。 例如,我有一些书,它们都存储在该库中。 我的问题如下: 我应该使用图书馆类作为模型来保存我的书籍对象吗? 像这样:

class Library {
    static addBook(book){
        this.books.push(book);
    }

    static getBooksList() {
        return this.books;
    }
}

还是创建Library的抽象类然后创建对象并将该对象用作存储更好?(模型)

1 个答案:

答案 0 :(得分:2)

最灵活的方法是不假设只有一个库,即使您当前的需求可能是这样。

因此,我不会在您的库类上使用静态方法,而是使用库 instance 。这样一来,它可以更好地代表现实生活-一本书可以一天存放在图书馆A中,另一本书可以存放在图书馆B中。

{
  "a11": {
    "a22": {
      "colours": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      },        
      "animations": {
        "value": {
          "title": "title here",
          "priority": 2
        }
      },
      "fonts": {
        "value": {
          "title": "title here",
          "priority": 3
        }
      },
      "visibility": {
        "value": {
          "title": "title here",
          "priority": 4
        }
      }
    },
    "b22": {
      "logo": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      }
    },
    "c22": {
      "unordered": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      },        
      "define": {
        "value": {
          "title": "title here",
          "priority": 2
        }
      },
      "ordered": {
        "value": {
          "title": "title here",
          "priority": 3
        }
      },
    },
    "d22": {
      "head": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      },        
      "foot": {
        "value": {
          "title": "title here",
          "priority": 2
        }
      },
    },
    "e22": {
      "headings": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      },        
      "blockquote": {
        "value": {
          "title": "title here",
          "priority": 2
        }
      },
      "inline-elements": {
        "value": {
          "title": "title here",
          "priority": 3
        }
      },      
      "hr": {
        "value": {
          "title": "title here",
          "priority": 4
        }
      },
      "preformatted": {
        "value": {
          "title": "title here",
          "priority": 5
        }
      },      
      "paragraph": {
        "value": {
          "title": "title here",
          "priority": 6
        }
      },
      "time": {
        "value": {
          "title": "title here",
          "priority": 7
        }
      }
    }
  },
  "b11": {
    "f22": {
      "menu": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      }
    },
    "g22": {
      "product-item": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      }
    },
    "h22": {
      "search": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      }
    },
    "i22": {
      "sub-menu": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      }
    }
  },
  "c11": {
    "j22": {
      "footer": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      },
      "title": {
        "value": {
          "title": "title here",
          "priority": 2
        }
      }
    },
    "k22": {
      "header": {
        "value": {
          "title": "title here",
          "priority": 1
        }
      }
    }
  }
}

如果书籍数量很大,则应考虑使用class Library { constructor(name) { this.name = name; this.books = []; // Instantiate the books array. } addBook(book) { this.books.push(book); // Some extra handling if a book can only be in one library if (book.library) book.library.removeBook(book); book.library = this; } removeBook(book) { let i = this.books.indexOf(book); if (i === -1) return; this.books.splice(i, 1); book.library = null; } getBooksList() { return this.books; } } class Book { constructor(title, author) { this.title = title; this.author = author; this.library = null; } } const book = new Book("1984", "George Orwell"); const library = new Library("The London Library"); library.addBook(book); console.log("The book " + book.title + " is in " + book.library.name); const otherLibrary = new Library("The Library of Birmingam"); otherLibrary.addBook(book); console.log("The book " + book.title + " is in " + book.library.name);代替Set使用Array。它将在 O(1)时间而不是 O(n)清除图书。