Is there any possibility to have JSON.stringify preserve functions?

前端 未结 9 1874
一个人的身影
一个人的身影 2020-12-01 05:14

Take this object:

x = {
 \"key1\": \"xxx\",
 \"key2\": function(){return this.key1}
}

If I do this:

y = JSON.parse( JSON.st         


        
9条回答
  •  既然无缘
    2020-12-01 06:04

    The naughty but effective way would be to simply:

    Function.prototype.toJSON = function() { return this.toString(); }
    

    Though your real problem (aside from modifying the prototype of Function) would be deserialization without the use of eval.

提交回复
热议问题