Case 1 :- Initially in object literal I assign __proto__
to null and then I'm trying to reassign it to animal
, which is not working.
'use strict'
let animal = {
eats: true,
walk(){
console.log('Animal Walk');
}
}
let rabbit = {
jumps: true,
__proto__: null
}
rabbit.__proto__ = animal
console.log(Object.getPrototypeOf(rabbit))
rabbit.walk()
Case 2:- Now i assign __proto__
in object literal as animal
and later i try to reassign it to null
, it changes __proto__
to null
in this case.
'use strict'
let animal = {
eats: true,
walk(){
console.log('Animal Walk');
}
}
let rabbit = {
jumps: true,
__proto__: animal
}
rabbit.__proto__ = null
console.log(Object.getPrototypeOf(rabbit))
rabbit.walk()
Case 3:- When i use Object.setPrototypeOf()
it works fine
'use strict'
let animal = {
eats: true,
walk(){
console.log('Animal Walk');
}
}
let rabbit = {
jumps: true,
__proto__: null
}
Object.setPrototypeOf(rabbit, animal)
console.log(Object.getPrototypeOf(rabbit))
rabbit.walk()
Why isn't the assignment working in the first case?