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 questionsThe 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.
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.
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)
Keep practicing
Take the free CRT-600 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CRT-600 practice questions
- Question 2A developer has the function, shown below, that is called when a page loads. function onl…
- Question 3A developer needs to test this function: 01 const sum3 = (arr) => ( 02 if (!arr.length) r…
- Question 4Refer to the code below: Const resolveAfterMilliseconds = (ms) => Promise.resolve ( setTi…
- Question 10Refer to the following code block: class Animal{ constructor(name){ this.name = name; } m…
- Question 11A developer needs to debug a Node.js web server because a runtime error keeps occurring a…
- Question 12Refer to the string below: const str = 'Salesforce'; Which two statements result in the w…
