我怎样才能让这段Javascript代码更加干燥?

发布于 2025-01-19 18:09:45 字数 686 浏览 3 评论 0原文

这是奥丁项目的一项作业。当我进行测试时,我意识到这段代码很糟糕,因为它不是 DRY。我只会创造越来越多的书籍对象。我怎样才能让这个更干净?

let myLibrary = [];

    function Book(title, author, pages, hasRead){
      this.title = title;
      this.author = author; 
      this.page = pages;
      this.hasRead = hasRead;
    };
    
    function addBookToLibrary(newBook){
      return myLibrary.push(newBook)
    };
    
    let newBook = new Book("Wool", "Hugh Howey", "592", false);
    let lifeOfPi = new Book("Life of Pi", "Yann Martel", "392", false);
    
    addBookToLibrary(newBook);
    addBookToLibrary(lifeOfPi);
    console.log(myLibrary);

This is for one of the assignments on Odin Project. As i'm testing things out, i'm realizing how this code is bad because it's not DRY. I would just be creating more and more book objects. How can I make this more clean?

let myLibrary = [];

    function Book(title, author, pages, hasRead){
      this.title = title;
      this.author = author; 
      this.page = pages;
      this.hasRead = hasRead;
    };
    
    function addBookToLibrary(newBook){
      return myLibrary.push(newBook)
    };
    
    let newBook = new Book("Wool", "Hugh Howey", "592", false);
    let lifeOfPi = new Book("Life of Pi", "Yann Martel", "392", false);
    
    addBookToLibrary(newBook);
    addBookToLibrary(lifeOfPi);
    console.log(myLibrary);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2025-01-26 18:09:45

你可以做这样的事情,这样你就有了一个“BookLibrary”类。

// Make the class "BookLibrary"

var BookLibrary = function() { 

    
    this.myLibrary = [];


    this.addBookToLibrary = function(title, author, page, hasRead) {

        var newBook = {}; // a book new object to be stored to array

        newBook.title = title;
        newBook.author = author;
        newBook.page = page;
        newBook.hasRead = hasRead;

        this.myLibrary.push( newBook ); // add to collection of books

    }
    
}

// test demo

// instantiate the object 
var books = new BookLibrary();

// add some books
books.addBookToLibrary("Wool", "Hugh Howey", "592", false);
books.addBookToLibrary("Life of Pi", "Yann Martel", "392", false);

// see if it worked
console.log( books.myLibrary );

You could do something like this, so that you have a "BookLibrary" class.

// Make the class "BookLibrary"

var BookLibrary = function() { 

    
    this.myLibrary = [];


    this.addBookToLibrary = function(title, author, page, hasRead) {

        var newBook = {}; // a book new object to be stored to array

        newBook.title = title;
        newBook.author = author;
        newBook.page = page;
        newBook.hasRead = hasRead;

        this.myLibrary.push( newBook ); // add to collection of books

    }
    
}

// test demo

// instantiate the object 
var books = new BookLibrary();

// add some books
books.addBookToLibrary("Wool", "Hugh Howey", "592", false);
books.addBookToLibrary("Life of Pi", "Yann Martel", "392", false);

// see if it worked
console.log( books.myLibrary );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文