From the course: JavaScript: Patterns
Unlock the full course today
Join today to access over 22,700 courses taught by industry experts or purchase this course individually.
Factory pattern - JavaScript Tutorial
From the course: JavaScript: Patterns
Factory pattern
- [Instructor] The factory pattern is great when you want to create, say, a mechanism to create other objects. Good example would be a factory for creating cars. This can be useful when you want a factory to handle most of your classes and then simply use this factory method to create your new objects. Again, this is best demonstrated in a code example. So let's go back to our index.js and what I'm going to do is refactor some of that code to resemble what we had before for the class car. So I'm going to basically remove the if and else, like so. And remove the return instance. And now we should be good to go. I'm just going to re-indent this properly and remove the space in here. So basically your class car should resemble something like this. So basically what we had before. Constructor passing the doors, engine, color, and then boom, boom, boom. And, of course, let's remove the instance here, like so. And remove it at…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.