Can anyone help me to undrstnd the output of the below code

  let obj1 ={
    fName : 'Ayush',
    lName : 'Singh',
    city: 'Asansol',
    getName : function(){
      console.log(`I am ${this.fName} ${this.lName} from ${this.city}`)
    }
  }
  let obj2 = {
    fName : 'Aman'
  }

  obj2.__proto__ = obj1;

  console.log(obj1.getName())
  obj2.getName()
  console.log(obj2.__proto__.getName())
  console.log(obj1.__proto__.getName())

Here I am trying to check how proto works. Why can’t I access of obj1.proto.getName

>Solution :

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

how proto work is it as getter and setter of object.

why your obj1.proto doesnot work because you have not set it. you only set it for obj2.

more ref from docs :-

Object.prototype.proto

Leave a Reply