DEV Community


Discussion on: Automate your getters and setters with Proxies

stephengfriend profile image
Stephen G. Friend

Hi Amin, can you expand on why you chose to use the same get trap for the setter? Is there an advantage to not putting this in the set trap?

aminnairi profile image
Amin Author • Edited

Hi Stephen and thanks for your question which is interesting!

The set trap will be triggered when you set a property for the object that is being proxied.

motorcycle.brand = "Triumph";

But when using the setter method motorcycle.setBrand("Triumph"); you are really just triggering the get trap, whether it is a property or in this case a method. Using a set trap wouldn't have helped us much.

Hope this answers your question.

Forem Open with the Forem app