CRT-600 practice question 26 of 30
A developer has two ways to write a function: Option A: function Monster() { This.growl = () => { Console.log (“Grr!”); } } Option B: function Monster() {}; Monster.prototype.growl =() => { console.log(“Grr!”); } After deciding on an option, the developer creates 1000 monster objects. How many growl methods are created with Option A Option B?
Full set: 223 questionsIn Option A, the growl method is defined inside the Monster constructor and assigned directly to this. This means that every time a new Monster object is instantiated using the new keyword, a new, unique function object for growl is created in memory and attached to that specific instance. Therefore, creating 1000 Monster objects results in 1000 separate growl methods. In Option B, the growl method is defined on the Monster constructor's prototype. This creates a single growl function that is shared among all instances of Monster via the prototype chain. Each of the 1000 instances will reference this one shared method, rather than having its own copy, which is more memory-efficient.
A. This option incorrectly reverses the outcomes. The prototype method is shared (1), while the instance method is duplicated (1000).
C. This is incorrect because the two approaches have fundamentally different implications for memory allocation and method creation.
D. This is incorrect because only the prototype-based approach (Option B) results in a single, shared method being created.
1. Mozilla Developer Network (MDN) Web Docs, "Object prototypes": In the section "Adding properties and methods to objects," it explains that adding methods to the prototype is more efficient. It states, "A method defined on a constructor's prototype property is shared among all instances of that constructor's type... In contrast, if a method is defined in a constructor, a new function is created and assigned for every object instance." This directly supports the reasoning for both Option A and Option B.
2. Mozilla Developer Network (MDN) Web Docs, "new operator": The documentation for the new operator explains that when a constructor is called, a new object is created, and this is bound to that new object. Any assignments to this (like this.growl = ...) become properties of that unique instance, confirming that a new function is created for each object in Option A.
3. Flanagan, D. (2020). JavaScript: The Definitive Guide (7th ed.). O'Reilly Media. In Chapter 9, "Classes," the section on "Prototypes" explains that methods defined on the prototype are inherited and shared by all instances, whereas properties defined in the constructor are instance-specific. This principle underlies the memory difference between the two options.
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…
