ES6: The Right Parts Dumping Variables
Transcript from the "Dumping Variables" Lesson
>> Kyle Simpson: So, if we know that we can receive assignments without declaration. We can also do an interesting little trick, it's just a little side note, it's not something I'm doing an awful lot. But you remember earlier, when we we're talking about using block scoping to do that whole swapping thing.
[00:00:24] Remember we did let tmp = x; x =y; y = tmp. Remember that? We can use array de-structuring to do the swapping. Let's say I had an x = 10. And a y that was 20 and I wanted to swap the two. Could do that whole block thing that I just showed.
[00:00:44] Or what I could do is say, an array de-structuring with x and y as my pattern should be de-structured from an array that has them in the opposite order.
>> Kyle Simpson: And of course that's not limited to two. You could do as many of those as you wanted to, right?
>> Kyle Simpson: While we're on the topic of using this array de-structuring stuff, If we had an a that was an array like that.
>> Kyle Simpson: We could say, for example.
>> Kyle Simpson: So, now we're making an array, a composite array, I'll use spaces just to make this a little more readable.
[00:01:44] We now have an array on the right hand side, that has zero through four in it, right? And over here we could say, I want to throw away the first two of these. We're going to have to make variables to catch those, collect the rest of them right back into a.
[00:02:01] So, that is the effect of throwing away zero and one, and now a is going to have two three four in it, okay? Remember our whole usage of dot, dot, dot operator? It also works with our de-structuring. So, now we see the dot, dot, dot on both sides.
[00:02:15] One on the right hand side it's doing spreading. On the left hand side it's doing gathering.
>> Kyle Simpson: And we could just have a single variable here, like that or underscore, or whatever other one you wanted to use to say, this is just, I'm just dumping these I don't care about, okay?
>> Kyle Simpson: But you don't even need that, because the array de-structuring allows you to do empty slots.
>> Kyle Simpson: So, now if we ran this example, our a would be an array that had values two three and four in it. Zero and one would have been done, okay? So, we can use our pattern de-structuring and even more, I don't necessarily want to call it a tricky way, but we can use the capabilities of it to take on tasks that would have been a lot more ugly to express in imperative form.
>> Speaker 2: So, I take it if you transpired that it would just give it some random variable names that never get used again? On those two that get tossed up.
>> Speaker 2: It just slices instead.
>> Kyle Simpson: See they're doing all this analysis to figure out what's the simplest path, used characters, most performant to get it.
[00:03:41] So, you might say, this is how I would do it I'd make these temporary variables or whatever, and they're saying we've all ready analyzed and figured out this is the best way to do it.
>> Speaker 2: So, with the empty slots just the white space matter, or is it just [CROSSTALK]
>> Kyle Simpson: I do that for readability, it doesn't matter at all.
>> Kyle Simpson: There's the array with two, three, four in it.