CRT-600 practice question 27 of 30

Given the code below: 01 function GameConsole (name) { 02 this.name = name; 03 } 04 05 GameConsole.prototype.load = function(gamename) { 06…

Full set: 223 questions
Given the code below: 01 function GameConsole (name) { 02 this.name = name; 03 } 04 05 GameConsole.prototype.load = function(gamename) { 06 console.log( ` $(this.name) is loading a game : $(gamename) …`); 07 ) 08 function Console 16 Bit (name) { 09 GameConsole.call(this, name) ; 10 } 11 Console16bit.prototype = Object.create ( GameConsole.prototype) ; 12 //insert code here 13 console.log( ` $(this.name) is loading a cartridge game : $(gamename) …`); 14 } 15 const console16bit = new Console16bit(‘ SNEGeneziz ’); 16 console16bit.load(‘ Super Nonic 3x Force ’); What should a developer insert at line 15 to output the following message using the method ? > SNEGeneziz is loading a cartridge game: Super Monic 3x Force . . .
Answer options
A: Console16bit.prototype.load(gamename) = function() {
B: Console16bit.prototype.load = function(gamename) { Correct answer
C: Console16bit = Object.create(GameConsole.prototype).load = function (gamename) {
D: Console16bit.prototype.load(gamename) {
Correct Answer:
B. Console16bit.prototype.load = function(gamename) {
Explanation

The goal is to override the load method inherited from GameConsole.prototype for instances of Console16bit. This is achieved by defining a new load method directly on the Console16bit.prototype object. Option B, Console16bit.prototype.load = function(gamename) {, uses the correct syntax to assign a new function to the load property of the child's prototype. When console16bit.load() is called, the JavaScript engine finds this new function on Console16bit.prototype first in the prototype chain and executes it, producing the desired specialized output.

Why Incorrect

A. Console16bit.prototype.load(gamename) = function() { is invalid syntax. It attempts to assign a function to the result of a method call, which is not a valid assignment target.
C. Console16bit = Object.create(GameConsole.prototype).load = function (gamename) { is incorrect because it reassigns the Console16bit constructor itself, rather than modifying its prototype.
D. Console16bit.prototype.load(gamename) { is syntactically incorrect. This shorthand method syntax is only valid within class definitions or object literals, not for direct prototype assignment.

References

1. Mozilla Developer Network (MDN) Web Docs: The official documentation for JavaScript explains how setting a property on a more specific object in the prototype chain "shadows" the property from the less specific object. "When you set a property to an object

the property is created on the object itself... If a property with the same name exists on the prototype chain

this is called shadowing."

Source: MDN Web Docs

Object prototypes

"Shadowing properties" section. developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritanceandtheprototypechain#shadowingproperties

2. Mozilla Developer Network (MDN) Web Docs: The documentation on Object.create() explains its role in setting up prototypal inheritance

as used on line 11 of the provided code

which is the foundation for the method override.

Source: MDN Web Docs

Object.create()

"Using Object.create() for prototypal inheritance" section. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/GlobalObjects/Object/create#usingobject.createforprototypalinheritance

3. University of Washington

Paul G. Allen School of Computer Science & Engineering Courseware: University course materials on JavaScript detail the mechanism of overriding methods by re-defining them on the "child" constructor's prototype.

Source: CSE 154: Web Programming

Lecture 13: JS Objects and Prototypes

Slide 34 ("Overriding Methods"). courses.cs.washington.edu/courses/cse154/18au/lectures/13/lecture13-js-objects-prototypes.pdf

Scroll to Top