专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

高级前端必会设计模式之原型模式

ins518 2024-09-11 09:27:34 技术文章 24 ℃ 0 评论

前端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()

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表