CRT-600 practice question 18 of 30

At Universal Containers, every team has its own way of copying JavaScript objects. The code Snippet shows an implementation from one team: Function…

Full set: 223 questions
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?
Answer options
A: Hello Dan Doe
B: Hello John DOe
C: TypeError: dan.name is not a function Correct answer
D: TypeError: Assignment to constant variable.
Correct Answer:
C. TypeError: dan.name is not a function
Explanation

The core of this question lies in the behavior of JSON.stringify(). This method converts a JavaScript object to a JSON string, but it only serializes data properties. According to the ECMAScript specification, properties whose values are functions, undefined, or Symbols are omitted during the serialization of an object. In the provided code, the name property of the john object is a function. Therefore, it is excluded from the JSON string created by JSON.stringify(john). When this string is parsed back into an object with JSON.parse(), the resulting dan object has the firstName and lastName properties but lacks the name method. The subsequent call to dan.name() attempts to invoke an undefined property, which results in a TypeError.

Why Incorrect

A. Hello Dan Doe: This is incorrect because the name method is not transferred to the dan object during the JSON serialization/deserialization process.
B. Hello John Doe: This is incorrect for the same reason as option A. The dan object does not inherit or copy the name method.
D. TypeError: Assignment to constant variable.: This is incorrect. The const declaration prevents the variable dan from being reassigned, but it does not make the object it references immutable. Modifying a property of the object (dan.firstName = 'Dan') is a valid operation.

References

1. Mozilla Developer Network (MDN) Web Docs: In the documentation for JSON.stringify(), under the "Description" section, it states: "If undefined, a Function, or a Symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array)." This directly confirms that the name function will be omitted. (Source: MDN Web Docs, JSON.stringify())

2. ECMAScript® 2022 Language Specification (ECMA-262, 13th Edition): The official standard for JavaScript defines the serialization process. In section 25.5.2.1 SerializeJSONObject ( state, value ), step 4.c describes iterating through an object's properties. Sub-step ii specifies that if the property's value is undefined, a Symbol, or a Function, the property is skipped and not included in the output string. This formalizes the behavior observed in the question. (Source: ECMA International, ECMA-262 Standard, Section 25.5.2.1)

Scroll to Top