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

相关文章:

  • 2021-07-24
  • 2021-07-22
  • 2021-08-25
  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2021-05-11
  • 2022-12-23
相关资源
相似解决方案