search

Home  >  Q&A  >  body text

javascript - The object has attributes with set and get methods set in the prototype chain. This attribute will also be automatically written into the object.

1: If the attributes of the set and get methods are set in the prototype chain of an object, the object will automatically write this attribute.

2: The code is as follows

let test = {
        a:1,b:2
    }
    Object.defineProperty(test,'a',{
        set(){
            return 'set'
        },
        get(){
            return 'get'
        }
    })
    let o = {};
    o.__proto__ = test;

3: View o

in the console


#4: Why does object o also have a property?

世界只因有你世界只因有你2740 days ago873

reply all(1)I'll reply

  • 欧阳克

    欧阳克2017-06-28 09:28:57

    Seeing is not necessarily believing...

    If you don’t believe me, try executing Object.getOwnPropertyDescriptor(o, 'a') and Object.getOwnPropertyDescriptor(test, 'a') respectively.

    This should be just the effect created by Chrome's debugging tool to facilitate the display. This situation will not occur under Firefox.

    reply
    0
  • Cancelreply