189k views
1 vote
What is the drawback of creating true private methods in JavaScript?

A. True private methods have no drawbacks; they are always the best choice.
B. True private methods can't be used within the same object and are isolated from the rest of the code.
C. They can't access public methods and properties in the same object.
D. They can lead to increased memory usage in the JavaScript engine.

User RJnr
by
8.7k points

1 Answer

1 vote

Final answer:

The main drawback of using true private methods in JavaScript is that they can lead to increased memory usage since each object instance gets its own copy of the method.

Step-by-step explanation:

The drawback of creating true private methods in JavaScript is D. They can lead to increased memory usage in the JavaScript engine. When true private methods are defined inside of a class constructor or a method that creates an instance, a new copy of the method is created each time an object is instantiated. This contrasts with public methods, which are typically placed on the prototype and shared across all instances, leading to more efficient memory use. Therefore, excessively using true private methods within classes or functions that create multiple instances can significantly increase the memory footprint of your application.

User GabAntonelli
by
8.0k points