原型模式(Prototype Pattern)
定义:用于创建重复的对象,同时又能保证性能。
目的:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
场景:在运行期建立和删除原型。
let productPrototype = {
init: (type) => {
this.type = type
},
getType: () => {
return this.type
}
}
let prototype = (type) => {
function F () {
}
F.prototype = productPrototype
let f = new F()
f.init(type)
return f
}
let car = prototype('丰田CHR')
console.log(car.getType())
Github地址:github.com/skillnull/D…