Created
January 6, 2020 13:47
-
-
Save arifmahmudrana/1e7aa7702868b648c68281b16a5d2cbe to your computer and use it in GitHub Desktop.
custom toJSON example
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
// custom toJSON example object | |
const a = { | |
id: 1, | |
name: 'ARIF', | |
toJSON() { | |
return JSON.stringify({id: this.id}) | |
} | |
} | |
const a = new A(); | |
const b = {...a} | |
console.log(JSON.stringify(a)) | |
console.log(JSON.stringify(b)) | |
// custom toJSON example class | |
class A { | |
constructor() { | |
this.id = 1; | |
this.name = 'ARIF' | |
} | |
toJSON() { | |
return {id: this.id} | |
} | |
} | |
const a = new A(); | |
const b = {...a} | |
console.log(JSON.stringify(a)) | |
console.log(JSON.stringify(b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment