# 题目描述

实现bject.create数的功能且该新函数命名为_objectCreate"

# 测试用例

function test() {
  let obj = {};
  const a = _objectCreate(obj);
  return a.__proto__ === obj;
}
// true
1
2
3
4
5
6

# 思路

Object.create创建一个新对象,使新对象的proto__指向传入的参数对象

  1. 创建一个临时函数
  2. 将该临时函数的原型指向对象参数
  3. 返回该临时对象的实例

# 代码实现

function _objectCreate(proto) {
  if (typeof proto !== "object" || proto == null) {
    return;
  }
  const fn = function () {};
  fn.prototype = proto;
  return new fn();
}

function test() {
  let obj = {};
  const a = _objectCreate(obj);
  return a.__proto__ === obj;
}

console.log(test());
// true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17