Avoid Assignment Errors by Using Reflect.set

June 03, 2019 0 Comments

Avoid Assignment Errors by Using Reflect.set



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.

Tag cloud