The ES6 syntax takes some getting used do, when we’ve been working with and getting comfortable with “traditional javascript”. But there are a lot of advantages to the new syntax. And this particular lesson is a very simplistic use of destructuring. Consider a larger example. Suppose I had a JS Object something like this:
let student = {
_id: "015fb23a44fcca9a0",
name: {
first: "Robert",
middle: ["Robin", "Reuben"],
last: "Roberts"
},
courses: [
{
id: "CSA002.2",
name: "Computer Science 02.2",
instructors: [
{
_id: "ab883ec0a526fac",
name: {
first: "Ray",
last: "Stevens"
}
},{
_id: "83ec0a526facab8",
name: {
first: "Samuel",
last: "Davis"
}
}
]
}
]
}
Granted, this is a very truncated example of a JSON object for a student – there might be more courses, or grades, or whatever. But the point remains: If you wanted to use this in a function to create a student mailer (for example), you js function likely would pull information out of that and use it in a template. How might that happen?
function createMailerTemplate(studentObject){
return `Dear ${studentObject.name.first} ${studentObject.name.last},
It has come to our attention that one of your courses, ${studentObject.courses[0].name}, has been cancelled. So sorry for the inconvenience.
`;
}
Now in that, I’m using the values from the studentObject
directly, which is a pain in the butt and a WHOLE lot of unneccesary typing. Instead, using the javascript we already know, we could:
function createMailerTemplate(studentObject){
let first = studentObject.name.first,
last = studentObject.name.last,
courseId = studentObject.courses[0].id,
courseName = studentObject.courses[0].name;
return `Dear ${first} ${last},
It has come to our attention that one of your courses, (${courseId}) ${courseName}, has been cancelled. So sorry for the inconvenience.
`;
}
Looks a little better, at least in the template portion, right? But we still have that really REALLY ugly bit at the top where we pull out the parts we need from our object. And that’s where using destructuring comes in. It is simply a shortcut way of doing the exact same thing we did in that last example. Instead of hard-coding the path into each variable assignment, we can destructure them from the object directly into the variables we want. In fact, we can even go one step further with destructuring, and do it right in the function signature, where we define the parameters:
// Look at the parameter signature -- it says we're still expecting an object, but it's pulling
// bits of the object DIRECTLY INTO VARIABLES!! MAGIC!!
function createMailerTemplate({first, last, courses}){
// we can filter the array to get the one course we want...
let course = courses.filter(course => course.id == "CSA002.2");
return `Dear ${first} ${last},
It has come to our attention that one of your courses, (${course.id}) ${course.name}, has been cancelled. So sorry for the inconvenience.
`;
}
As I said, the given example is trivial, and really doesn’t seem to serve a purpose. It mostly introduces you to the idea that you can, with a handy shorthand, pull data from an object into multiple variables at a time. It is a handy trick, and it has made many of my code bits a LOT cleaner.