嗨,有人可以帮助解释为什么我的代码不起作用吗?



function Books(title, author, pages, info) {
this.title = title
this.author = author
this.pages = pages
this.info = function() {
return this.title + 'by' + this.author + ',' + this.pages + 'pages' + ',' + 'have read!'
}
}
var books = new Books('Behold a Pale Horse', 'Bill Cooper', 505)
books.info()

我也能够在这里毫无问题地运行它。我建议进行一些编辑,只是为了让代码和打印文本更可读:

function Books(title, author, pages, info) {
this.title = title;
this.author = author;
this.pages = pages;
this.info = () => {
return this.title + ' by ' + this.author + ', ' + this.pages + ' pages' + ',' + ' have read!';
}
}

var books = new Books('Behold a Pale Horse', 'Bill Cooper', 505);

console.log(books.info());

它正在工作,可能你的意思是它什么都不打印?

尝试

console.log(books.info())

尝试:

console.log(books.info())

您的代码是绝对正确的
它没有在控制台上打印输出的原因是你没有使用console.log(books.info())
如果你使用Chrome的控制台,你甚至不需要使用console.log()

相关内容

最新更新