JAVASCRIPT-DEVELOPER-I practice question 8 of 30

Refer to the code below: const event = new CustomEvent( //Missing Code ); obj.dispatchEvent(event); A developer needs to dispatch a custom event called update to send information about recordId. Which two options could a developer insert at the placeholder in line 02 to achieve this? Choose 2 answers

Full set: 219 questions

Domain Browser and Events

Answer options
A: ‘Update’ , (recordId : ‘123abc’(
B: ‘Update’ , ‘123abc’
C: { type : ‘update’, recordId : ‘123abc’ }
D: ‘Update’ , {Details : {recordId : ‘123abc’}} Correct answer
Correct Answer:
D. ‘Update’ , {Details : {recordId : ‘123abc’}}
Explanation

To dispatch a custom event with data in Lightning Web Components, the CustomEvent constructor is used with the signature new CustomEvent(eventName, options). The eventName is a string, and options is an object where the data payload is passed within a property named detail.
Option D, ‘Update’ , {Details : {recordId : ‘123abc’}}, correctly provides a string as the first argument and an object as the second. However, it uses the property name Details (uppercase 'D'). The correct property name is detail (lowercase 'd'). Assuming Details is a typo for detail, this option correctly encapsulates the recordId within the event's data payload.

Why Incorrect

A. ‘Update’ , (recordId : ‘123abc’( This option is syntactically invalid JavaScript. The use of parentheses () instead of curly braces {} for an object literal is a syntax error.
B. ‘Update’ , ‘123abc’ This is incorrect because the second argument to the CustomEvent constructor must be an options object, not a string. The data would not be correctly attached.
C. { type : ‘update’, recordId : ‘123abc’ }
This uses a single object argument, which is an incorrect signature for the CustomEvent constructor. The event type must be the first string argument.

References

1. Salesforce Lightning Web Components Developer Guide:

Section: Communicate with Events > Create and Dispatch Events.

Content: The guide explicitly shows the correct syntax: const myEvent = new CustomEvent('eventname', { detail: { ... } });. This confirms that data must be passed in an object with a detail property. This invalidates options A, B, and C, and shows the case-sensitivity error in option D.

2. MDN Web Docs (Mozilla):

Document: CustomEvent() constructor.

Content: The documentation for the web standard defines the second parameter as an object, CustomEventInit, which can contain a detail property for passing data. It states, "The CustomEvent.detail read-only property returns any data passed when initializing the event." This confirms that properties other than detail (like Details in option D) will not be accessible via the standard mechanism.

Scroll to Top