js

  

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const prototype = {
getName() {
return `first-name: ${this.firstName}, last-name: ${this.lastName}`
},
say() {
console.log('love you')
}
}

// 克隆自己,生成一个新的对象
// Object.create()用到了原型模式的思想
// 基于一个原型创建一个对象

let x = Object.create(prototype)
x.lastName = 'shulu'
x.firstName = 'lqy love'

x.say() //love you
console.log('x.getName() :', x.getName())

//打印为 x.getName() : first-name: lqy love, last-name: shulu

QQ有事您Q我👇