js设计模式学习笔记(九):原型模式

50 阅读1分钟

原型模式

特点

  • 根据已有对象,clone 出一个新对象, 以此减少创建对象所产生的性能消耗。这里针对创建性能消耗较大的对象

介绍

  • 与其说是一种模式,更不如说是一种编程泛型。

demo

更多详细代码


const prototype = {
    getName:function(){
        return `${this.firstName}-${this.lastName}`
    }

    say:function(){
        console.log("hello world!");
    }
}

const A = Object.create(prototype);

A.firstName = "A";
A.lastName = "B";

console.log(A.getName()); // A-B

A.say(); // hello world