3 ms·
This is the slow, inefficient way of doing OOP in JS. Pros: 1. You don't have to understand JavaScript to use it 2. You get to have "private" members 3. You
by itsnotlupus 15y ago
This is the slow, inefficient way of doing OOP in JS.
Pros:
1. You don't have to understand JavaScript to use it
2. You get to have "private" members
3. You don't have to use "this". See point #1.
Cons:
1. Object churn: Every "new" operation creates an object that you proceed to immediately discard
2. Speed: Object creation is slow.
3. Maintenance: Object inheritance is quirky. Lack of "protected" members ends up forcing you to rewrite your "private" members as "public" one, negating pro #2.
4. OOP: The "instanceof" operator won't work on your instanciated objects. You'll need to define your own introspection mechanism to tell which instance comes from which class.
My take:
- It has a lot in common with the module pattern, commonly used to namespace things.
- It's okay to use it, or something like it, for objects that get relatively few instances.
- If you're going to ignore "this" in your constructor, don't use the "new" operator to create your objects.
- Don't use this pattern for something that gets instantiated a lot.
- Think carefully before using this pattern if you intend to use any kind of class inheritance with it.