ES6基础知识——设置对象的prototype Object.setPrototypeOf()
作者:互联网
Object.setPrototypeOf()可以在创建对象以后去改变对象的prototype
例子:这里定义了两个对象:
let breakfast = { getDrink(){ return 'tea' } } let dinner ={ getDrink(){ return 'bear' } }
下面使用Object.create()方法创建一个基于breakfast的对象sunday,可以看到sunday的getDrink()方法返回的就是breakfast对象里的getDrink()方法返回的字符串
let sunday = Object.create(breakfast) console.log(sunday.getDrink()); //tea //判断sunday这个对象的prototype是否等于breakfast console.log(Object.getPrototypeOf(sunday)===breakfast) //true
Object.setPrototypeOf(sunday,dinner); console.log(sunday.getDrink()); //bear console.log(Object.getPrototypeOf(sunday)=== dinner); //true
标签:ES6,getDrink,setPrototypeOf,console,Object,sunday,breakfast,prototype 来源: https://www.cnblogs.com/fe-cherrydlh/p/11094950.html