Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Avoid Assignment Errors by Using Reflect.set


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: 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.

    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.

    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.

    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.

    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.