I'm trying to define a non-enumerable toJSON
function on a prototype object without much luck. I'm hoping for something similar to ECMAScript 5 toJSON
:
Object.defineProperty(obj, prop, { enumerable: false });
However this defines it as a property which cannot be accessed as a method.
[EDIT: Nick is wrong; it can be accessed as a method. His mistake was in code that is not shown in this question - see his comments on answers below, for details.]
I was hoping to be able to define the function in a non-enumerable fashion, as I was planning to define in the prototypes of all primitive types (String
, Number
, Boolean
, Array
, and Object
), so that I can recursively apply the function through complex objects.
The end goal here is to be able JSONify a Backbone model/collection with nested collections recursively.
I guess in total I have two main questions:
- Is it possible to define a non-enumerable function on a prototype? If so how?
- Is there a better way to JSONify nested Backbone models?