ES6基础之——__proto__
__proto__:前面两个下划线,后面两个下划线,ES6的__proto__可以的得到或者设置对象的prototype
使用__proto__可以直接在对象表达式里面设置prototype,例子:
let breakfast = { getDrink(){ return 'tea' } } let dinner ={ getDrink(){ return 'bear' } } let sunday = { __proto__:breakfast } console.log(sunday.getDrink()); //tea
//判断sunday这个对象的prototype是否等于breakfast
console.log(Object.getPrototypeOf(sunday)===breakfast) //true
使用__proto__设置对象的prototype
sunday.__proto__=dinner; console.log(sunday.getDrink()); //bear console.log(Object.getPrototypeOf(sunday)=== dinner); //true