Skip to content

Instantly share code, notes, and snippets.

@deleteman
Created March 28, 2019 02:19
Show Gist options
  • Save deleteman/b81d701fb8e0e666e8537a1ebad1bf53 to your computer and use it in GitHub Desktop.
Save deleteman/b81d701fb8e0e666e8537a1ebad1bf53 to your computer and use it in GitHub Desktop.
tojson serialization
let testObj = {
name: "Fernando",
age: 35,
speak: function() {
console.log("Hello world!")
},
toJSON: function() {
console.log("toJSON called")
},
address: undefined
}
let testObj2 = {
name: "Fernando",
age: 35,
speak: function() {
console.log("Hello world!")
},
toJSON: function() {
console.log("toJSON called")
return '{ "name": "' + this.name + '", "age": ' + this.age + ' }'
},
address: undefined
}
let serializedObj = JSON.stringify(testObj)
let serializedObj2 = JSON.stringify(testObj2)
testObj.speak()
console.log(serializedObj)
console.log(typeof serializedObj)
console.log(" - - - - - - - - - - - - - ")
console.log(serializedObj2)
console.log(typeof serializedObj2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment