Reflect.set
enables the same behavior as simply assigning a property to a value, but because it's a function, it allows you to check if the assignment completed successfully. This allows you to avoid errors when assignment fails in scenarios where the Object might be frozen or sealed.
Instructor: [00:00] When you create an object and create a property on the object. Person name is John. I'll just log that out, console log person.name. You can see that we just logged out John.
[00:16] If now we seal the object, so object.seal person, it will give us an error saying that this object is not extensible, we can't add a property to it.
[00:28] We want to avoid that error because in our code, we might not know where this is defined and whether or not it is sealed. The solution to that is using the Reflect API, so reflect set person, and then the name key, and then the value, John. This does not throw an error, but it invokes a function. If we check the output, we'll see that the output is false.
[00:55] If the object is not sealed -- I'll comment that out -- we get the output of true, and we get John. If it is sealed, we get the output of false and undefined.
[01:06] Reflect set allows us to see if this operation will work. It won't just error out. That gives us information vital to avoiding errors in our project.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!