前端JavaScript设计模式-原型模式:
原型模式是指通过克隆自己,创造一个原型对象,通过原型生成多个新对象
// 原型对象
const prototype = {
getName:function(){
return this.first + ' ' + this.last
},
say:function(){
console.log('say hello')
}
}
// 通过原型创建A对象
let a = Object.create(prototype)
a.first = 'a'
a.last = 'A'
console.log(a.getName())
a.say()
// 通过原型创建B对象
let b = Object.create(prototype)
b.first = 'b'
b.last = 'B'
console.log(b.getName())
b.say()
本文暂时没有评论,来添加一个吧(●'◡'●)