Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

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


    ES6 Parameter Object Destructuring with Required Values


    Not only can you provide default values when using ES6 parameter object destructuring, but you can also require the presence of certain properties



    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




    What we have here is an AJAX function using ESX parameter object to structuring and we're providing default values. As you can see, when we call our AJAX function we're getting not only the values we passed into the function, but we're also getting default values for the things we didn't pass like type, data, and is async.

    We could, at this point, remove the URL and success properties when calling the AJAX function, and you'll see we're providing default values for them, but that doesn't seem quite right. Those properties should probably be required, so let's do that.

    What we're going to do is come up here and create a new function called required parameter, and give it a name of parameter. Then we're going to throw a new error, and it will say, "Missing parameter." It'll say, "Name."

    Instead of setting a default value for the URL property, we're going to call our required parameter function instead. What will happen is, if our AJAX function is called without a URL property, the required parameter function will be invoked instead resulting in an error exception saying, "Missing parameter URL." Likewise, we could do the same thing for the success property.

    Now, if we try to call the AJAX function with only the URL property, we should see a missing parameter exception for the success property. If we provide a value for the success property, then we should see all of our parameters logged to the console. By leveraging the default value feature of object destructuring to our advantage, we could provide required parameters as you've seen demonstrated.