Which two blocks of code are needed to implement a custom getter in a Lightning web component?
Correct Answer: A,D
Explanation
custom getter in a Lightning web component is a JavaScript function that executes logic each time a public property is accessed. A custom getter must start with the keyword get and be followed by a name for the property. A custom getter must also have a corresponding custom setter, which is a function that executes logic each time a public property is assigned a value. A custom setter must start with the keyword set and have the same name as the getter. One of the getter or setter functions must be annotated with @api, which makes the property public and reactive.
Option A and Option D show the correct syntax for defining a custom getter and setter for a public property called name. Option A shows the getter function, which returns the value of a private property called _name.
Option D shows the setter function, which assigns the value of the parameter value to the private property _name. The getter function is annotated with @api, which makes the name property public and reactive.
Option B and Option C are incorrect because they do not follow the syntax for a custom getter and setter.
Option B shows a regular function declaration, not a getter function. Option C shows a regular assignment statement, not a setter function. Neither option uses the @api decorator, which is required for a public property. References:
* Getters and Setters
* Understand getter in Lightning Web component