JS.Next: ES6 / ES2015 JS.Next: ES6 / ES2015

Exercise: Default Parameters

Check out a free preview of the full JS.Next: ES6 / ES2015 course:
The "Exercise: Default Parameters" 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 leads the audience through a code example that uses a method as the default value. This method returns a counter that is incremented when no parameter is specified. He also demonstrates how exceptions could be used when no value is present and other gotchas (like lexical scope) that can arise.

Get Unlimited Access Now

Transcript from the "Exercise: Default Parameters" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Aaron Frost: So let's code that up real quick, so that we can have an example of how it's gonna work. So it's a,
>> Aaron Frost: Function. So let's give us a counter, var counter = 0, function getCount,
>> Aaron Frost: Is return counter++, and then we're gonna say, function logMe,
>> Aaron Frost: count or getCount.

[00:00:52] Just so you guys can see this happening every single time, actually.
>> Aaron Frost: Okay, so yeah.
>> Aaron Frost: Dude, I wrote the word in there, count, like ten times. I'm sorry, this is not a very good code example. So, I'm gonna call logMe, and we'll pass it 10, it logs at 10, right?

[00:01:30] So, if we say, logMe, and we don't pass it anything,
>> Aaron Frost: It said, I'm getting the counter and it's 0, okay, and,
>> Aaron Frost: If we call it again, if what I'm saying is right, it will say getting the count, it will log that getting the counter again, which means it's gonna call every single time this triggers.

[00:02:00] And it did. So I'm getting the counter. So every single time that you've got a violation of that thing, it's gonna go ahead and re-trigger that event. Does that answer any of the question? Okay, are you happy that happens, or does that bother you?
>> Off Camera 1: I'm just surprised.

[00:02:18]
>> Aaron Frost: You're surprised?
>> Off Camera 1: Because I was assuming it's a static value, just like when you had a string there whatever. But I mean, it makes sense. It's just a little surprising because that means they actually put in the logic in there, and they do parsing to say, hey, this is not a value, this is a call coming.

[00:02:41]
>> Aaron Frost: Yeah, it is actually executed, as opposed to evaluated one time.
>> Off Camera 2: And if you wanted it the other way, you'd have to make a separate call before that, before defining that function, just to have what the-
>> Aaron Frost: Yeah, and again if you don't like that, you don't have to use default parameters.

[00:03:00] I think that they looked at what are people doing most of the time, let's do that. And I think most of the time, people want that thing to run every time. So, that is a good thing that you had us test though cuz I didn't actually cover that.

[00:03:12] Thank you.
>> Off Camera 1: Yeah, actually, your slide there actually kind of does the.
>> Aaron Frost: This thing?
>> Off Camera 1: Just in a different way, right, cuz this is kind of-
>> Aaron Frost: Yeah, yeah, yeah, this is the same kind of idea, right? So one thing I wanted to show is that if you get an exception in the method that's triggered by the default assignment, it's gonna kill your execution.

[00:03:39] So it's not immune to exceptions. Like, it's the same as if you put it inline, and it got an exception there, so, yeah. Eduardo shivers when he sees ++.
>> Off Camera 1: [LAUGH]
>> Aaron Frost: My code has made him shiver. I'm gonna take it as a compliment. Okay, I mean, I have a method here called die.

[00:04:04] I'm like, it's supposed to die every single time. No matter how many times I called test, if I don't pass it something, it's gonna die on purpose. So this is something you could do as well. If you had something that you needed them to provide you, you could have a die function if you wanted to.

[00:04:20] Or where you're like, just give up on life, end it all right now, tell them that they did it wrong. So this is something you could do, I mean, I'm not saying it's bad or good, it's just a thing. Okay, again, they propagate the errors though.
>> Aaron Frost: So it says assignment, yeah, yeah, yeah.

[00:04:48] It's a good slide, whoever wrote it was great. Anyway, so it says assignment happens lexically, so I wanna just kinda point out what's gonna happen here. So I have a global x, right, out here, and it's just a string, it's not important, it's init. And down here, we have this test function where if I don't pass anything in, it's gonna say u equal to x.

[00:05:13] Okay, and then internally, I have a var x. And I say go ahead and return a. So test doesn't do anything besides it takes a thing and just hands it back, right? And then so when I call test, and I don't pass anything, it's gonna go get x.

[00:05:32] So everyone here's like, cool, x = "INIT", right, but it's happening lexically, and inside this lexical scope, x is still undefined. Okay, so actually, it assigned it to undefined. And it's not gonna like do this endless loop. So, if you're like default assignment function returns an undefined, it's not gonna just sit there and call it until it gets a not undefined.

[00:06:01] It's just gonna call it one time, so that make sense? Good? Okay, awesome. Okay, so yeah, I mean, again, no, x, not a good variable name. Use better variable names than that, but hopefully, you're not gonna have too many name collisions like this, but if you did this, it would be why.

[00:06:25] Not all parameters need default values. So, if you're gonna assign one, you don't have to assign the next one, it is optional. So that's cool. And then you can't use default parameters with the rest, though. So we've got rest here, right, where if I call this function and I pass it 100 things, rest will be the array of those 100 things.

[00:06:51] Whereas if I don't pass it anything, or I pass undefined, it's not gonna let this happen. You can't use this. This is gonna give you a syntax error. Okay, so don't try and use default parameters with rest. If you had, I'm gonna copy this real quick and show you, cuz I don't want to be misinterpreted.

[00:07:16]
>> Aaron Frost: So if you had a, b, you could still use it over here.
>> Aaron Frost: You just can't use it over here. That's the only syntax error, okay, is to put it on the rest parameter itself. So, you can use it every while, it's just not on the rest parameter, eye to eye?

[00:07:41] We're good, okay. Awesome, is everyone online good?
>> Off Camera 3: Mike, how long do you have in this section left?
>> Aaron Frost: A few minutes.
>> Off Camera 3: Okay, perfect.
>> Aaron Frost: Okay, so, another thing that's unique about default parameters, so, when you have the arguments object, so let's say I called test, and I don't pass it anything.

[00:08:07] It wants an a, a b, and a c. So, if I don't pass it anything, a's gonna be 1, b's gonna be 2, c's gonna be 3, so they're all three gonna have values, right? But arguments.length is 0, okay? So just because it has a value, it's still not gonna throw it into the arguments object.

[00:08:26] So if you're one of those wizards that sits there and checks incoming parameters versus the arguments object, and you know who you are, this may throw you for a loop unless you like that it does that. So, anyway. So, if I pass it 1 because I only passed it 1, a's gonna still be 1, b and c will both have values, but the arguments was only one line.

[00:08:51] So, we're all good, right? Default parameters do not show up inside the arguments object.
>> Off Camera 3: There was a question there right at the end.
>> Aaron Frost: Can get hot count have a reference to this? If so, what would it be?
>> Aaron Frost: Can getCount have a reference to this? I'm gonna go to Firefox for this because it's actually implemented here.

[00:09:17] So,
>> Aaron Frost: Can getCount have a reference to this?
>> Aaron Frost: I mean, I think it's just stuck. I'm gonna just say this. I'm not even gonna try it out. It's just a regular method call. So, it's whatever the this is. If you're daisy chained off of an object call, this is the object.

[00:09:48] Anywhere else, you're gonna be the window, so yeah, I'm gonna answer it that way. So we define default parameters from left to right. I mean, you can, I mean, if I imply that, I apologize. Let me show this real quick. Like if I skip this one, I can still do the next one.

[00:10:20] I wasn't trying to say once you do one, you have to do the rest. That's not what I'm trying to say. Just whatever order you wanna do them in.
>> Aaron Frost: Yeah, I think it looks gross, too, but whatever. Yeah, like that doesn't look great, but you can do it.

[00:10:37] Not everything has to have a default value, just, only the ones you want sans the rest argument.