2017-04-25 67 views
1

嘗試使用構造函數來使用按鈕單擊來更新書籍的對象類。我不斷收到錯誤,指出找不到3個變量(book1,book2和book3)。不知道爲什麼,但我相信它是一個非常簡單的錯誤(或者可能是一個非常激烈的錯誤)。我希望將信息顯示在警報中,但是可以使用document.getElementById()。innerHTML =;更新在瀏覽器中出現錯誤,說找不到變量?

<!DOCTYPE html> 
<html> 
<head> 
    <title>JavaScript Book Object</title> 
</head> 
<body> 
    <h1>Two Examples of Books</h1> 
    <h3>Read Available Books</h3> 
    <input type = 'button' value = 'To Kill a Mockingbird' onclick = 'book1.read();'/> 
    <input type = 'button' value = 'The Outsiders' onclick = 'book2.read();'/> 

    <h3>Read in your own custom book</h3> 

    <input type = 'button' value = 'Enter your book' onclick = 'book3.promptNewBookInfo();'/> 
    <input type = 'button' value = 'Your Book' onclick = 'book3.read();'/> 

    <script type = "text/javascript"> 
     var book1 = new Book("To Kill a Mockingbird", "Harper Lee", "David Johnson", 281, "Social Drama"); 
     var book2 = new Book("The Outsiders", "S. E. Hinton", "(none)", 192, "Young Adult Fiction"); 
     var book3 = new Book("tbd", "tbd", "tbd", 0, "tbd"); 

     function Book(ti, au, ill, pgs, gnr) { 
      this.title = ti; 
      this.author = au; 
      this.illustrator = ill; 
      this.pages = pgs; 
      this.genre = gnr; 

      this.read = funcion() { 
       var newBook = this.title + " by" + this.author + "and illustrated by:" + this.illustrator + " is" + this.pages + " pages long" + "and is " + this.genre; 
       alert(newBook); 
      }; 
      this.promptNewBookInfo = function() 
      { 
       this.title = prompt("What is the title?"); 
       this.author = prompt("Who is the nook written by?"); 
       this.illustrator = prompt("Who is the book illustrated by?"); 
       this.pages = parseInt(prompt("How many pages is the book?")) 
       this.genre = prompt("What genre is your book?"); 
       alert (this.title + "is now ready to be read!"); 
      } 
     } 

    </script> 
</body> 
</html> 
+0

什麼是'找不到'?它返回'undefined'嗎? –

回答

0

拼寫錯誤:funcion =>功能

this.read = function() { 
       var newBook = this.title + " by" + this.author + "and illustrated by:" + this.illustrator + " is" + this.pages + " pages long" + "and is " + this.genre; 
       alert(newBook); 
      }; 
+0

太近了......哈哈謝謝大家:) –

0

它只是一個拼寫錯誤

this.read = funcion() { 

應該

this.read = function() { 

繼續努力吧!

相關問題