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.
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?
I don't get it, why can't you access it as a method?
If you wanted it on the prototype, it's as easy as
or even directly in
Object.create
However, unless you're creating instances of
foo
, it won't show up if you try tofoo.bar
, and only be visible asfoo.prototype.bar
.If
foo
has it's own prototype (e.g.foo = Object.create({})
), you can get it withObject.getPrototypeOf
, add the property to that and thenfoo.bar
would work even if it is not an instance.You can see visibility of enumerable vs non-enumerable properties here.
Always you can avoid enumberable functions properties in object when you looping through it. And instead of define property in each object and set enumberable to false , you can create function which will call to any object with the property you want and put a condition to not take the property in the looping list. here is the example :
Paul S. is right about needing to set the property definition's
value
instead of aget
, but I wanted to add that you don't need to passenumerable: false
, because false is the default for that option inObject.defineProperty()
The answer can be simplified to: