Cannot use function (arrow or anonymous) from object after stringify and then parsing

Viewed 27

I was just playing with JS and I came across this strange behavior.

Code is as follows

let a = {
    "b" : 1,
    "a" : function(){
    return 1;
  }
}

let b = {
    "b" : 2,
    "a" : ()=>{
    return 2;
  }
}


let x = JSON.stringify(a);
console.log("For a ",JSON.parse(x));

x = JSON.stringify(b);
console.log("For b ",JSON.parse(x));

As you can see the object containing the value as a function gets removed automatically after we parse the stringified object.

I was wondering why and how it happened ??

0 Answers
Related