Skip to content

Instantly share code, notes, and snippets.

@WesTyler
Created February 17, 2017 21:28
Show Gist options
  • Save WesTyler/97e90b867c7fd4c8dbca2818f19dd145 to your computer and use it in GitHub Desktop.
Save WesTyler/97e90b867c7fd4c8dbca2818f19dd145 to your computer and use it in GitHub Desktop.
JSON stringify bug with prototype conflict
'use strict';
// Observed in Node v4.5.0 and v5.0.0
// Does not appear to affect Node versions >= 6.x
const Thing = function(input) {
this.value = input;
Object.defineProperty(this, 'nonEnum', {value: 'this should not serialize'});
};
Thing.prototype.nonEnum = 'Overwrite the nonEnum to trigger the bug';
const thing = new Thing('my value');
JSON.stringify(thing);
// '{"value":"my value"}'
JSON.stringify(thing, null, null);
// '{"value":"my value","nonEnum":"this should not serialize"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment