2017-04-25 46 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() { 

继续努力吧!

相关问题