ganqqwerty / 123-Essential-JavaScript-Interview-Questions

JavaScript interview Questions
BSD 3-Clause "New" or "Revised" License
5.01k stars 1.18k forks source link

"Answer" Question 3 is misleading -- and potentially wrong. #28

Closed chief10 closed 6 years ago

chief10 commented 6 years ago

Question 3. What is the drawback of creating true private in JavaScript?

The "solution" says each copy of Employee will have its own copy of the increaseSalary and that such an implementation would be poor for memory. While that is true, it is also true that each instance of Employee would also have it's own copy of any properties or methods added to this. So in the case of your example, for the purposes of memory, increaseSalary is no different from dispalyIncreasedSalary in terms of memory consumption.

To actually be more memory efficient, one would want to put common properties and methods on the Employee prototype.

https://stackoverflow.com/questions/8433459/what-s-the-purpose-of-prototype

ganqqwerty commented 6 years ago

You're absolutely right about this. I will fix this question. Thanks for helping the book!