手写Object.create
注意
手写 Object.create
答: 原理:本质上是返回一个对象,对象的构造函数的原型是create传入的第一个参数。
function create(obj, propertiesObject = {}) {
function F() {}
const res = new F()
res.__proto__ = obj
Object.defineProperties(res, propertiesObject)
return res
}
// 或者
// fucntion F() {}
// F.prototype = obj
// const res = new F()
// 测试
create({}, { name: { value: 'zcl' } })
Object.create({}, { name: { value: 'zcl' } })
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

编辑 (opens new window)
上次更新: 2025/07/20, 06:21:22