2

I'd sure like to be able to add this:

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

But it gets all circular reference-y. I still want to be able to write .toJson() all over the everywhere.

9
  • Maybe with out that this.toJson = undefined in there. That was messing around that ended up in my clipboard. Commented Oct 8, 2013 at 23:40
  • Just drop the this.toJson = undefined;...what are you trying to accomplish with that? Commented Oct 8, 2013 at 23:40
  • Without it still yeilds: Uncaught TypeError: Converting circular structure to JSON it was a lame attempt to make that go away. Commented Oct 8, 2013 at 23:43
  • 1
    @nclu: Can you please show us on what object you tried .toJson()? Probably it just has a circular reference. Commented Oct 8, 2013 at 23:55
  • 2
    Don't forget to have a look at How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop Commented Oct 8, 2013 at 23:57

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.