Object.create

71 阅读1分钟

Object.create() 方法用于创建一个新对象,使用现有的对象来作为新创建对象的原型(prototype)。


const obj = {

name:"deep",

age:23,

toLogMessage:function(){

console.log(`我是${this.name},我的年龄是${this.age}`);

}

}

const newObj = Object.create(obj)

newObj.age = 18

newObj.toLogMessage()

手写Object.create

function create(obj){

function F(){}

F.prototype = obj

return new F()

}