This course has been updated! We now recommend you take the ES6: The Right Parts course.
Transcript from the "Rest Parameters" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Aaron Frost: Rest parameters, what are they? Has anyone heard of Splat? Splat. Okay, CoffeeScript developers, okay. Anybody ever hear of VARARGS? You heard of that? Java developers, no? Not so much. Params? Yeah, C# developers, okay, all right. So does anyone still not know what Rest Parameters is, like based off what I said?
[00:00:28] I got some confused looks. Okay, let's talk about it. Let's show it off then. This is Rest Parameters, right there, okay? So you just, you prefix one of your parameter names with dot dot dot, that's how you create a rest parameter, and then the way you use it is like this.
[00:00:56] So I would have this Rest parameter called bar, and I'm going to log bar.join. Because what it's gonna do is it's gonna take all incoming arguments and it's going to put them into an array called bar. Okay, and I can do array things to it like join, right.
[00:01:13] So I'm going to say I, can, haz, teh, arguments It will go ahead and put that into a string for me. That makes sense? What is doing it's just, it's allowing you to catch all the parameters in one into an array. So, simple, right? It's not too hard, I think.
[00:01:36] It loads them all into an array called bar in this example. And you can use it just like you would any array. And some of you guys are like, my gosh, I finally have an object that I can capture all the arguments onto, right? I've never had that before.
[00:01:56] So, no one's thinking this, cuz we've always had that already, right? We've already been able to do this, essentially, right? Because we've had the arguments object, right? We've always been able to talk to the arguments object. Yeah, did you have a-
>> Speaker 2: But, is this a proper array or is this the arguments object in the array objects?
[00:02:19]
>> Aaron Frost: So I'm gonna talk about that. It is a proper array, yeah, and so I'm gonna point out the differences here. So with arguments, arguments isn't a natural array. Okay, it's array-ish, you poke it and you can see it has a length property, so you think that you might be able to call joint on a but it freaks out cuz it's not an array.
[00:02:38] It's a pseudo array, almost. And so in order to kind of give you the same thing, you'd have to join it out here, or you could always do something that's even more confusing. I'll show you what you could do. You could say array.prototype.join.call(arguments) and that would do the exact same thing, as calling arrary.join, okay.
[00:03:14] So, you could always do this. You could always pass arguments to the array prototype and it usually can figure out what to do, but not always. But, anyways, so yeah, that's kind of where I was going was, we always have been able to do this. It's not a big deal, right?
[00:03:30] It's array-ish, it has a length. I get things out of arguments by index, right. It's got three things in it, I can get the first one by arguments sub 0 arguments sub 1 arguments sub 2. So I kind of treat it like an array in some aspect, but it's totally not an array, right?
[00:03:48] And so any of the array methods aren't there and if you call them then you're gonna get an error. So we've always been able to have the arguments, but now we kind of have a different way to do it that's, I think, better. And let me explain why.
[00:04:02] So,
>> Aaron Frost: Yeah, here's what I was saying earlier. I forgot I had this slide, sorry. But yeah, you could call array.prototype.join.call(arguments). Yeah you have to pass it the space so that it knows what to join on. Anyway, so yeah, so you've always been kind of able to do the same kind of things.
[00:04:29] There's some major differences, though. The rest argument only includes non-specified arguments, okay? So arguments includes every argument, regardless of whether you specified it or not, okay. Let me kind of give an example of that. So, with this argument function, argumenty, I have name and if I console log name and arguments.
[00:04:58] It's gonna go ahead, well actually, let's go to run this in Firefox so everyone can kind of see.
>> Aaron Frost: Can you guys see that? Is that big enough? Okay so I'm going to go ahead and run this and we'll look at the outcome. So when this first was called, it logged the name, like we thought it was, but it also logged the arguments, and the name was in the arguments.
[00:05:40] You see that? So I kind of got the name two times, right?
>> Aaron Frost: Look over here. And then in the second one, where I used the rest parameters, I put in the name and then all the other ones over here. And when I look at it, I got the name over here.
[00:05:58] And when you look at this array, the name's not in there right any more. Only the last name and the city and state and stuff. So you don't, it just picks up the rest of the stuff, not everything like the arguments object. So that's one of the major differences.