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 questionsThe 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.
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.
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
Keep practicing
Take the free CRT-600 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CRT-600 practice questions
- Question 2A developer has the function, shown below, that is called when a page loads. function onl…
- Question 3A developer needs to test this function: 01 const sum3 = (arr) => ( 02 if (!arr.length) r…
- Question 4Refer to the code below: Const resolveAfterMilliseconds = (ms) => Promise.resolve ( setTi…
- Question 10Refer to the following code block: class Animal{ constructor(name){ this.name = name; } m…
- Question 11A developer needs to debug a Node.js web server because a runtime error keeps occurring a…
- Question 12Refer to the string below: const str = 'Salesforce'; Which two statements result in the w…
