介绍
※ clone 自己,生成一个新对象
※ java 默认有 clone 接口,不用自己实现
实现代码
//一个原型 对象
const prototype = {
getName: function () {
return this.first + ' ' + this.last
},
say: function () {
alert('hello')
}
}
//基于原型创建X
let x = Object.create(prototype);
x.first = 'A'
x.last = 'B'
alert(x.getName())
x.say()
//基于原型创建y
let y = Object.create(prototype);
y.first = 'A'
y.last = 'B'
alert(y.getName())
y.say()
对比JS中的原型prototype
● prototype 可以理解为ES6 class 的一种底层原理
● 而 class 是实现面向对象的基础,并不是服务于某个模式
● 若干年后ES6 全面普及,大家可能会忽略掉prototype
● 但是Object.create却会长久存在