简体   繁体   中英

Hi can someone help explain why my code isn't working?

 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()

I was also able to run it with no problems here. I would suggest a few edits, just to make the code and printed text more readable:

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

it is working, probably you meant it does not print anything?

try

console.log(books.info())

Try:

console.log(books.info())

Your code is absolutely correct.
The reason why it isn't printing the output on the console is that you haven't used console.log(books.info())
If you use Chrome's console you wouldn't even need to use console.log()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM