Q: 7
At Universal Containers, every team has its own way of copying JavaScript objects. The
code
Snippet shows an implementation from one team:
Function Person() {
this.firstName = “John”;
this.lastName = ‘Doe’;
This.name =() => (
console.log(‘Hello $(this.firstName) $(this.firstName)’);
)}
Const john = new Person ();
Const dan = JSON.parse(JSON.stringify(john));
dan.firstName =’Dan’;
dan.name();
What is the Output of the code execution?
Options
Discussion
Option C JSON.stringify only copies data, not functions, so dan.name is undefined. Trying to call it throws a TypeError. Saw a similar question in practice sets.
Option C. pretty sure about this since JSON.stringify drops functions. Official guide and dev playgrounds both cover this behavior.
C. Tried this before and JSON.stringify strips the methods, so dan.name is undefined. TypeError every time you call it. Someone correct me if they see a different edge case.
Saw something similar in official docs and some practice sets, this kind of copy won’t preserve methods so it’s C.
Nah, it's not A like some might think. C is correct here since JSON.stringify skips methods, so dan has no name function.
C or D, but I'd say D since const reassignment throws before the call.
Yeah, definitely C. JSON methods drop the function so dan.name isn’t there to call.
Call it C, had something like this in a mock and it threw TypeError when calling dan.name.
Be respectful. No spam.