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 questions
Answer options
A: 1 growl method is created for Option
B: 1000 growl methods are created for Option B. Correct answer
C: 1000 growl method is created for Option
D: 1 growl methods are created for Option B.
E: 1000 growl methods are created regardless of which option is used.
F: 1 growl method is created regardless of which option is used.
Correct Answer:
B. 1000 growl methods are created for Option B.
Explanation

In 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.

Why Incorrect

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.

References

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.

Scroll to Top