You must create an object to represent each library book, storing its title, author, and a method that prints those details. Which example correctly defines such an object?
{ "Gulliver's Travels", "Jonathan Swift", function displayBookInfo() { console.log('Displaying book information'); } }
The correct example is an object literal that declares two string properties (title and author) and one method (displayInfo) whose value is a function. The second option omits property names, so the literal is syntactically invalid. The third option is an array, which merely stores indexed elements; although arrays can hold functions, they do not provide named properties for title and author. The fourth option assigns functions to title and author and stores a string in displayInfo, so it does not meet the requirement of string properties plus a method.
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 is an object in programming?
Open an interactive chat with Bash
What is the difference between properties and methods in an object?
Open an interactive chat with Bash
What does `this` keyword mean in JavaScript objects?