Loading ...
Sorry, an error occurred while loading the content.

543Re: [json] json.js breaks for-in loops

Expand Messages
  • Tom Metro
    Oct 14, 2006
    • 0 Attachment
      Peter Michaux wrote:
      > By extending the Object.prototype with the new property toJSONString I
      > can no longer use for-in loops in my JavaScript.

      As another poster mentioned, using hasOwnProperty(), is the way to make
      the intended behavior work. My understanding is that hasOwnProperty()
      isn't widely supported yet.

      > Has someone released a version of JSON that doesn't do this?

      I thought I remembered seeing a version that had a flag controlling
      whether the methods got added to the stock objects, but clearly that
      isn't in the current version of the code. I think I was thinking of
      another library... It'd be a worthy enhancement for the json.js library.

      > Is it just as simple as changing these
      > Object.prototype.toJSONString = function () {
      > to something like
      > JSON.objectToJSONString = function () {

      It looks like that should work. The code itself doesn't seem to
      recursively use those method names, instead using s[object type] internally.


      Tom Metro
      Venture Logic, Newton, MA, USA
      "Enterprise solutions through open source."
      Professional Profile: http://tmetro.venturelogic.com/
    • Show all 10 messages in this topic