javascript - 浏览器中出现错误,提示找不到变量?

标签 javascript html xcode function safari

尝试使用构造函数通过单击按钮来更新书籍的对象类。我不断收到错误消息,指出无法找到 3 个变量( book1book2book3 )。

不确定为什么,但我相信这是一个非常简单的错误(或者可能是一个非常严重的错误)。我希望信息显示在警报中,但我可以使用 document.getElementById().innerHTML = ;更新<p></p>

<!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>

最佳答案

拼写错误:funcion => function

 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);
            };

关于javascript - 浏览器中出现错误,提示找不到变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43599895/

相关文章:

javascript - jQuery 下拉菜单实现

javascript - 如何通过带有样式组件的 props 传递样式,而无需在组件内部创建组件

javascript - 鼠标滚轮移动时调用函数

html - 为我的网站制作代码框

c - Xcode 无法识别文件

javascript - 页面中的链接不起作用

javascript - Codeigniter:无限滚动不适用于数据库

xcode - 启用目标中缺少的位码构建选项

html - css如何使列自动填充背景颜色到高度100%

ios - 我可以使用滑动删除来使用 titleForDeleteConfirmationButtonForRowAtIndexPath 滑动来执行其他操作吗?