gpt4 book ai didi

javascript - 将 json stringify 添加到 javascript 对象原型(prototype)

转载 作者:行者123 更新时间:2023-11-30 05:43:43 29 4
gpt4 key购买 nike

我当然希望能够添加这个:

Object.prototype.toJson = function() {
this.toJson = undefined;
返回 JSON.stringify(this);
}

但是它得到的都是循环引用-y。我仍然希望能够在任何地方编写 .toJson()。

最佳答案

Seems to work fine.

Object.prototype.toJson = function() { this.toJson = undefined; return JSON.stringify(this); }

The error actually seems to come in when other libraries are later loaded. Moment.js and KendoUI both are triggering the circular reference. At this point I'm thinking that modifying the Object prototype isn't a very good idea. It'd be nice if objects could just serialize themselves and handle circularity in some magical elegant fashion

关于javascript - 将 json stringify 添加到 javascript 对象原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19260444/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com