JavaScript设计模式样例七 —— 原型模式

原型模式(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())

Git地址:https://github.com/skillnull/Design-Mode-Example

原文地址:https://www.cnblogs.com/Man-Dream-Necessary/p/12372165.html