我所理解的 Object.create

262 阅读1分钟
const person = {
        isHuman: false,
        printIntroduction: function () {
            console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
        }
    };
    // 创建的是一个空对象, 该对象的__proto__属性连接到 person 对象上面
    const me = Object.create(person); 
    console.log(me, 'me is wate') // {}
    console.log(me.isHuman, '我是普通的属性')  // false
    me.name = 'liba'
    me.isHuman = 'true'
    修改 me 对象的属性并不会影响 person 对象的属性
     console.log(person) // {isHuman: false, printIntroduction: ƒ} 
    console.log(me, 'me') // {name: "liba", isHuman: "true"}
    console.log(person.printIntroduction()) // My name is undefined. Am I human? false
    console.log(me.printIntroduction()) // My name is liba. Am I human? true

外面在下雨,喜欢下雨天。