Created
August 28, 2020 15:11
-
-
Save tomhodgins/1ed24e50aff165d82df2fb2c7aa729f2 to your computer and use it in GitHub Desktop.
You can define how an object stringifies with String() and toString(), and how it represents itself when converted to JSON
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
({ | |
value: 'hello world', | |
toString() { return `stringified that's: ${this.value}` } | |
}).toString() | |
// "stringified that's: hello world" | |
JSON.stringify({ | |
value: 'hello world', | |
toJSON() { return [{JSONified_thats: this.value}] } | |
}) | |
// "[{"JSONified_thats":"hello world"}]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment