原型模式(Prototype Pattern)
|
1 2 3 |
定义:用于创建重复的对象,同时又能保证性能。 目的:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 场景:在运行期建立和删除原型。 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const productPrototype = { init(type) { this.type = type return this }, getType() { return this.type } } function createProduct(type) { function F() {} F.prototype = productPrototype const product = new F() product.init(type) return product } const car = createProduct('丰田CHR') console.log(car.getType()) // 丰田CHR |
Github地址:https://github.com/skillnull/Design-Mode-Example[……]