# 18.手写 Object.create

  • 作用:Object.create() 方法用于创建一个新对象,使用现有的对象来作为新创建对象的原型(prototype)
const person = {
  isHuman: false,
  printIntroduction: function () {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  },
};

const me = Object.create(person);

me.name = "Matthew"; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // inherited properties can be overwritten

me.printIntroduction();
// expected output: "My name is Matthew. Am I human? true"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  • 实现
Object.prototype.myCreate = function (obj) {
  function Func() {}
  Func.prototype = obj;
  return new Func();
};
1
2
3
4
5
Last Updated: 6/3/2024, 1:08:34 AM