javascript setprototypeof code example

Example 1: isPrototypeOf js

/"Checks if an object exists in another object's prototype chain."/

function Bird(name) {
  this.name = name;
}

let duck = new Bird("Donald");

Bird.prototype.isPrototypeOf(duck);
// returns true

Example 2: object setprototypeof

const user = { name: 'John' };
const arr = [ 1, 2, 3 ];

console.log('Original state');
console.log(user);            // { name: 'John' }
console.log(user[1]);         // undefined
console.log(user.__proto__);  // {}
console.log(user.length);     // undefined

Object.setPrototypeOf(user, arr); // добавляем прототип arr к user

console.log('Modified state');
console.log(user);            // Array { name: 'John' }
console.log(user[1]);         // 2
console.log(user.__proto__);  // [ 1, 2, 3 ]
console.log(user.length);     // 3