课程: JavaScript: Patterns

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Factory pattern

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…

内容