Why doesn't JSON.stringify display object properties that are functions?
Because JSON cannot store functions. According to the spec, a value must be one of:
(source: json.org)
As a side note, this code will make the functions noticed by JSON.stringify
:
Function.prototype.toJSON = function() { return "Unstorable function" }
Here is another way with using a .prototype. You can add an function to stringify
JSON.stringify(obj, function(k, v) {
if (typeof v === 'function') {
return v + '';
}
return v;
});
It's not supposed to stringify methods (or any functions) - especially since most methods of built in objects (and thus the prototypes of any user-defined objects) are native code.
If you really need it to print your methods out, you can override your object's .toString method, but when you call JSON.parse on the stringified output, it will treat the method as if it were just a string, and to be able to call it as a function you'd have to eval
it - a practice that is typically not recommended.