5 ms·
The main reason I've come to avoid prototypes in many cases (except when I really need to control memory) is that you can't keep any kind of information private
by andrewstuart2 10y ago
The main reason I've come to avoid prototypes in many cases (except when I really need to control memory) is that you can't keep any kind of information private (via closure) and share it across instance methods. It has to be a property on the instance, and is therefore accessible to anybody with access to the instance.
Conventions (like prefixing with _) are great, but they can only help loosely enforce encapsulation and provide no form of real privacy.
- WorldMaker 10y agoYou can combine the two, creating a closure for private variables and using proper prototypes. The easiest way now is the ES2015 class keyword and its syntax, but for older versions of JS there are patterns and practices (and about a zillion helper libraries) to do so.
- learc83 10y agoI just use factory functions that return an object that has arrow functions as properties, then I get private information via closures, and I don't have to worry about dealing with this. https://www.youtube.com/watch?v=ImwrezYhw4w https://www.youtube.com/watch?v=ImwrezYhw4w