javascript - js中静态函数中传递对象作为参数

标签 javascript

我试图将新对象作为静态函数中的参数传递,但得到了意外的结果。 这是我的代码。但我想显示带有标题名称、作者姓名和 isbn 的表行。所以帮助解决这个问题。

    let bookTitle = document.querySelector("#title").value;
    let bookAuthor = document.querySelector("#author").value;
    let bookIsbn = document.querySelector("#isbn").value;
    
    class Book {
      constructor(title, author, isbn) {
        this.title = title;
        this.author = author;
        this.isbn = isbn;
      }
    }
    
    const makeNewBook = new Book(bookTitle, bookAuthor, bookIsbn);
    
    class MakeUi {
      static addTable(makeNewBook) {
        document.querySelector("#book-list").innerHTML = `<tr> 
                <td>  ${title} </td>
                <td>  ${author} </td>
                <td>  ${isbn} </td>
                <td>  <button class="btn btn-sm btn-danger"> X </button> </td>
           </tr>`;
      }
    }
    
    MakeUi.addTable(); 

这就是我得到的结果。
screenshot for the result

最佳答案

您需要首先从对象中获取值:

class MakeUi {
  static addTable(newBook) {
    const { title, author, isbn } = newBook; // Get the values from the object
    document.querySelector("#book-list").innerHTML = `<tr> 
                <td>  ${title} </td>
                <td>  ${author} </td>
                <td>  ${isbn} </td>
                <td>  <button class="btn btn-sm btn-danger"> X </button> </td>
           </tr>`;
  }
}

MakeUi.addTable(makeNewBook);

关于javascript - js中静态函数中传递对象作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58231084/

相关文章:

javascript - 绑定(bind) API 返回数据到下拉列表

javascript - 使用 AJAX post 将对象数组发送到服务器端

javascript - 从子组件更新父组件状态

javascript - vue.js 通过脚本传递 prop

javascript - QuietJs - Ionic 3 错误

javascript - Bootstrap : How can I change a modal's width with CSS without affecting other modals?

javascript - 在函数外部使用 $when...then() 内部的变量

javascript - 如何让一个函数只调用自己一次?

javascript - Ember.js 通过迭代从 json 生成 html

javascript - SVG 用类声明替换填充声明,用于高级颜色自定义