While working on a software project that manages a library's book inventory, you are asked to create an object to represent each book in the system. Each book should have information about its title, the author, and a function to display this information. Which of the following examples correctly defines such an object?
{ title: 'Gulliver’s Travels', author: 'Jonathan Swift', displayInfo: function() { console.log(this.title + ', written by ' + this.author); } }
{ 'Gulliver’s Travels', 'Jonathan Swift', function displayBookInfo() { console.log('Displaying book information'); } }
The correct answer defines an object with two properties (title and author) and a method (displayInfo). The method is a function that, when called, would be responsible for displaying the property values. The incorrect answers include either a standalone function (which does not associate with any book properties), an array, which cannot contain methods, or an object with incorrect datatype assignments (assigning a function to a property instead of methods).
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What are object properties and methods in programming?
Open an interactive chat with Bash
What is the difference between an object and an array in JavaScript?
Open an interactive chat with Bash
What is the purpose of a function within an object?