Crockford's Prototypal inheritance - Issues with nested objects

后端 未结 3 922
借酒劲吻你
借酒劲吻你 2020-11-22 01:21

I\'ve been reading \"Javascript: The Good Parts\" by Douglas Crockford - and while it\'s a bit extreme, I\'m on board with a lot of what he has to say.

In chapter

3条回答
  •  攒了一身酷
    2020-11-22 02:24

    I think what's happening is that when you create person2, the sex and info properties of it refer to those in nestObj. When you reference person2.info, since person2 doesn't redefine the info property, it goes through to the prototype and modifies the object there.

    It looks like the "right" way to do it is the way you build person3, so that the object has its own info object to modify and doesn't go up to the prototype.

    I'm reading the book too (slowly), so I sympathize with you. :)

提交回复
热议问题