Javascript JSON.stringify doesn't handle prototype correctly?

后端 未结 2 1857
情话喂你
情话喂你 2021-01-13 17:56

I\'ve been initializing my reusable classes like this (constructor is usually a copy-constructor):

function Foo() {}
Foo.prototype.a = \"1\";
Foo.prototype.b         


        
2条回答
  •  心在旅途
    2021-01-13 18:40

    your answer lies Why is JSON.stringify not serializing prototype values?

    JSON.stringify only does "own" properties.

    In your first example: prototype members are being set, and then calling Stringify on the object itself, which does not have its own properties.

    In your second: this.a will climb the chain until it finds the property

    In the third: You are setting properties directly on the object and not its prototype

提交回复
热议问题