Skip to content

Instantly share code, notes, and snippets.

@arifmahmudrana
Created January 6, 2020 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arifmahmudrana/1e7aa7702868b648c68281b16a5d2cbe to your computer and use it in GitHub Desktop.
Save arifmahmudrana/1e7aa7702868b648c68281b16a5d2cbe to your computer and use it in GitHub Desktop.
custom toJSON example
// 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