This course has been updated! We now recommend you take the ES6: The Right Parts course.

Check out a free preview of the full JS.Next: ES6 / ES2015 course:
The "Rest Argument Rules" Lesson is part of the full, JS.Next: ES6 / ES2015 course featured in this preview video. Here's what you'd learn in this lesson:

Aaron walks through a number of rules that exist when using rest parameters. There can only be one per function, it must be the last argument, and it can’t have default values. Aaron also take the audience through a couple code examples.

Get Unlimited Access Now

Transcript from the "Rest Argument Rules" Lesson

>> [MUSIC]

>> Aaron Frost: So there are some rules, you can only have one rest parameter per function, okay? Can't more than one. It's gotta be the last parameter, okay. It can't be the first one and I'll show you as why. And you can't use arguments anymore if you're gonna use it, okay.

[00:00:21] When a function evals is gonna say, do you have rest? Cool, then I'm gonna freak out if you use the word arguments. Unless you make a new variable called arguments, then it's okay. Is that confusing? A little bit. Anyway, don't ever make a variable called arguments. If you do, call me.

[00:00:40] And I'll come punch you in the neck. Anyway, yeah.
>> Speaker 2: [LAUGH]
>> Aaron Frost: Don't make a variable called arguments, right? It's not cool, okay. And you can't use default values on a rest parameter. And I'll show you guys default values in a different slide deck that we'll do later on.

[00:00:58] But, you can't use default values with the rest parameter. So let's take a look at each of these rules. They're all have some intricacies, I kinda want to point them out. So, you can only have one rest parameter per function. So, I want you guys look at this code and try and pretend that you're v eight.

[00:01:19] Or your spider monkey, okay. You are C++ now, okay. And you see that someone said okay, they want two rest params and they're passing all this stuff. So it's like, I don't know what things go in first and one thing is you going second. I don't know what you expected me to do here, right?

[00:01:45] So this is gonna die if you try to use more than one, okay? So I think that when I read that I was like, no not makes perfect sense, like you couldn't do more than two or more than one cuz it would know what to do and what so.

[00:02:00] So yeah you'll get an error that you've used multiple rest parameters. Questions on that one? No, pretty straightforward right. Okay it has to be the last one. So, that one I think make good sense too. At first I was like well, consider this example here. One, two, three, four, right.

[00:02:25] I'm gonna pass it in. Which one should go into bar and which one should go into biz. I got all no. If I call form, should I just read from the end, shake like read backwards? And am I just put the very last one in biz and then put these three in bar.

[00:02:46] But what if I do this? Which one should that go into? I don't know what to do. So, it's got to be the last one. And clearly for sanity and code readability purposes, okay. It's just your last parameter. It's not your first. It's not the middle one. In fact, if you try and do this you're gonna get an error, okay?

[00:03:06] All right, any questions on that? No, okay, so if you've got a rest parameter like this and you try and talk to arguments, as Cary said, it's gonna give you a syntax error cuz it actually doesn't provide the arguments object anymore when you've used a rest parameter. So, that's why you're gonna get an error saying, that variable hasn't even been declined or declared.

[00:03:30] Yeah?
>> Speaker 2: You can't get to call error. Those aren't part of the spec I guess. But It's like, arguments.caller get the function that called-
>> Aaron Frost: Do me a favor, try in the time that we have to track this stuff out and let me know. Is that cool? Okay, so, yeah, so like, if I call doSomething(1,2,3) and I try and access arguments in there, you're dead.

[00:04:00] Question on that one? Simple. No default values. So, default values is this is a new syntax we haven't talk about yet, but if I wanted to say, if the param is empty make it this array or I couldn't do that. There's no such thing as default values with this rest param.

[00:04:19] Okay, so you'll get a syntax error on that, any questions on that one? No, okay, it's rest parameters, that's a new functionality, you guys excited about that one? I'm stoked about that one. Okay, so try this out. Tried out on Firefox. Tried out on the Traceur Repl. Try and bug around with the arguments objects, see if you can get the, see if you get the caller and callee stuff like way he was saying.

[00:04:52] Just test getting those errors so you know what to expect. So you don't freak out when you get in like in the wild like don't use two of them make sure the last one. Trying get the errors to go like test the boundaries make sure that it is failing or should fail and just so you can getting it comfortable with how it works.

[00:05:09] So try it out, let's come back in, in ten minutes. Yeah, are there any questions?
>> Speaker 2: Yeah, before I ask it actually, are you going to talk about some of the new array features later at all?
>> Aaron Frost: No.
>> Speaker 2: Okay, so I mentioned in the chat, using array from an arguments to convert it into an array but that's also ES6 and one of the follow up question was why would we need an array from if we already have rest parameters?

[00:05:40] And in my mind it's just cause it works on other array like things, right? It doesn't have to be argument specifically.
>> Aaron Frost: Yeah, I don't know. That is a good question. Does array from, could you pass a set to array from?
>> Speaker 2: Anything that qualifies as that array like, where you can iterate it basically.

>> Aaron Frost: Yeah, that's a good question. I don't know man.
>> Speaker 2: I'm not sure about sets [CROSSTALK] [INAUDIBLE] or a set.
>> Aaron Frost: Yeah, you could try that out right now. I don't know. I mean Firefox and Chrome will have the new syntax. So, were there any other questions I didn't answer?

[00:06:18] I think we're caught up all asked. All right, so see that, see what it's writing there. Like they figured out a better way to do let. I'm gonna show you what they tried to do. And even this version of let will never, even when it compiles into it, it'll never respect.

>> Aaron Frost: The temporal dead zone, you'll always be like, how come I can still reference it before it was declared? Even if you transfile it with Traceur, it'll never be able to implement, at least with this version, a solid implementation. What it used to do for let was this.

[00:06:58] Does anyone know how you can get a temporally scoped variable mid block in encourage obviously because there's a way. And I actually I found it on here melts in my brain when I even thought about.
>> Speaker 2: You got to use like define property.
>> Aaron Frost: Let me show you.

[00:07:15] Dude, it was crazy. They would do this, this is not a joke. This is what they would do, okay?
>> Aaron Frost: I'm just gonna rewrite these three lines.
>> Aaron Frost: Guess what we have right there? We have a temporally scoped variable named e.
>> Aaron Frost: That's how they used to compile let.

[00:08:11] They were like, anytime you use it, they with throw, just to get this exception parameter they could then reassign to whatever variable value you set.
>> Speaker 3: I heard that it's also the way that Traceur, at the moment, is emulating let in the-
>> Aaron Frost: No, this is how they emulate right here.

>> Speaker 3: Okay, no, but in the browser, okay.
>> Aaron Frost: Yeah, yeah. They used to, totally. Like, that's actually how I found it. That's why when I saw them using it this way, I was like. That's interesting cuz it will never be able to properly, like function with a temporal deadzone idea.

[00:08:46] But I'm guessing it's way more maintainable. And I'm guessing it's more performing cuz I can imagine it's very performant to say stop execution and throw. I could imagine that's pretty costly. So, if it was trying to throw every so often, I can imagine that, that was like a good thing to do like on a large scale.

[00:09:12] So, this is probably more performant it's, for me, definitely more readable not that I would ever try to read this code anyway but yeah. If you use a grunt tracer like I said, and for those on the livestream. Using a grunt-traceur, you can tell it to provide you with source maps.

[00:09:30] And then in the browser, if you get an error, you can click on it and it will take you back to the original source. So yeah, did anyone have anything they found it was weird? All kinda what you expected, kinda normalish? Yeah? Someone had Firefox 8 on there.

[00:09:46] That was kind of weird. I'm guessing we got that figured out though.