Created
February 17, 2017 21:28
-
-
Save WesTyler/97e90b867c7fd4c8dbca2818f19dd145 to your computer and use it in GitHub Desktop.
JSON stringify bug with prototype conflict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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