ES6 basics - prototype Object.setPrototypeOf set objects ()

Object.setPrototypeOf () may be subject to change in the future created objects prototype

 

Examples: This defines two objects:
let breakfast = {
  getDrink(){
    return 'tea'
  }
}

let dinner ={
  getDrink(){
    return 'bear'
  }
} 

 

Use the following Object.create () method creates an object-based sunday breakfast, you can see the sunday getDrink () method returns the string is an object in the breakfast getDrink () method returns
let sunday = Object.create(breakfast)
console.log(sunday.getDrink()); //tea
// judge sunday prototype of this object is equal breakfast
console.log(Object.getPrototypeOf(sunday)===breakfast) //true

  



Re-set about the prototype sunday, using a Object.setPrototypeOf (), the first parameter is the object sunday to be set, and the second parameter is to be set into the prototype object dinner, return sunday.getDrink ()
Object.setPrototypeOf(sunday,dinner);
console.log(sunday.getDrink()); //bear
console.log(Object.getPrototypeOf(sunday)=== dinner); //true

  

Guess you like

Origin www.cnblogs.com/fe-cherrydlh/p/11094950.html