In this video I want to take a few minutes to explore another iOS 6 feature we're going to end up using
to fix a problem with our current application.
This is the default parameters syntax and that's going to allow us to set a default value for a function
parameter should no argument be passed in.
Now we'll explore this in isolation in the playground folder.
Then we'll integrate it into the application to fix a problem with our code.
So to kick things off we have one through six already in here.
I'm going to create a new script seven and I'll call this something like.
Default parameter dot J S.
And in here we're gonna start with probably the most basic function example you'll ever see.
Then we'll explore how we can improve it with default parameter values.
Let's go ahead and create a greeter function over going to do is accept a name as the first and only
parameter and then we're going to use it as part of a message we print down below.
So something like console dialog Hello space followed by the person's name so I'll concatenate on using
the plus operator the values stored in the name variable.
Now if we go ahead and call this we know exactly what's gonna happen and down below I'll do just that.
I'm going to call a greeter passing in a value for name.
I'll go ahead and use my name.
Andrew now from here we want to run the script so down below I am going to use controls C to shut down
node mine and we're gonna switch from the web server folder to the playground directory so S.D. dot
dot forward slash playground and once I'm inside of here I'm just gonna run the script.
So that would be node space then 7 default parameters dot J S.
Now when I run it I'm gonna see Hello andrew print.
No surprise there.
What I'm interested in though is what happens when I don't pass in an argument value to greeter.
So right here all I'm gonna do is call greeter with no arguments.
Now I'm gonna run the code once again and we're not going to get a javascript error.
The program isn't going to crash but the result we do get is just as undesirable right here.
When I run it we get.
Hello undefined.
So when the name is provided things go well when no name is provided.
Obviously we can't use it but we could definitely do better than this.
Instead of something like Hello undefined we could print something like Hello user or hello anonymous.
Now the reason we're seeing undefined here is because that is the default value for a function parameter.
If no argument is passed in none is provided here.
So name is undefined and I see undefined in that message.
Now obviously if we wanted to address this we could put an if statement inside of the greeter function
to check if the name exists.
If it does use it if it doesn't.
Inside of an else clause we could print a completely different message but we can also use the default
parameters syntax to achieve these same results with a lot less code and to set up a default value for
a function parameter like name.
All we do is use the equals sign right after it.
So for the moment let's go ahead and add a second argument into the mix just so it's easy to visualize.
So we take a name and we take an age.
The goal is to set a default value for a name.
What do we do.
We go ahead and we use equals.
Right after it followed by the default value we'd like to use.
This could be anything.
It could be a function an array an object a number a boolean or in our case a string.
Now what name do we want to use by default.
We could just use something generic like a user or anonymous or Hello person whatever you wanted to
use as that default string.
Now if I save things we can see how the program is going to function if an argument is provided that
value will be used which means we'll still see.
Hello Andrew.
If no argument is provided then the default value will be used which means for the second printing we
should see.
Hello.
User down below.
I'm going to go ahead and rerun the program.
After saving my file and that is exactly what I get I get Hello Andrew and Andrew is provided and I
get Hello user when no name value was provided.
Using this syntax makes it really easy to setup default values for your function parameters.
There's one more thing I want to talk about before we jump back into apt.
Jay asked and explore the issue we're going to address and what I want to talk about relates to dish
structuring.
So let's take a quick moment to open up that other playground file we created which was number five.
Yes six objects in here we explored how we could use the object property shorthand and we explored how
we could use object destruction bring we d structured as a standalone statement and we d structured
function arguments like we saw with our transaction function down below now in this case we were dis
structuring the second argument.
So I was expecting it to be an object.
Now currently it is an object we're passing in product.
But what would happen if I didn't pass in any value we know the default value would be undefined.
That's the value used if you don't pass anything in.
So what happened when we try to ditch structure undefined right here we can see what's going to happen
by running this script.
So I've removed that second argument in the call to transaction and I'll run node 5.
Yes six objects dot J.
S and when I do I get a javascript error.
Now the actual error message is cannot dish structure property label of undefined.
So what it's talking about here is this line that is line 32 which it mentions right here.
This is where we try to dish structure a parameter no value is passed in so undefined is used and we
cannot dish structure undefined.
Now we can easily address this by setting up a default parameter value.
So here we are de structuring undefined because no object was provided if we wanted to fix this we could
set a default value for that parameter equal to an empty object.
Now we're always going to get an object whether it's passed in or whether the default object is used.
Now let's run the program one more time to see what we get on line 33 when we log all three variables
out down below.
I'm gonna use the up arrow key and enter to rerun the previous command and what do I get for a name.
I get Andrew for age I get twenty seven and location Philadelphia.
That's coming from the first example for this console log.
I get the type value of order for a label I get undefined end for stock I get undefined as well so here
label and stock are both undefined because no object was provided.
So we d structure to the empty object and this object has no label or stock property.
So we see undefined for both.
So by setting up this default object we can make sure that when we d structure and object the code works
whether or not an object is ever passed in.
Now from here you could take things even further when you're D structuring.
You can also set up default values to use if you don't find a match.
So in this case let's say that for a stock I want to use zero if no stock property exists on the object
that's passed in like I would for a parameter.
I just set up equals afterwords and I pick a value.
Let's go ahead and use zero.
Now if we rerun the program we're going to see that order still prints for type undefined still prints
for a label and now stock is zero.
Now once again we're only seeing zero because no object is provided.
So we're de structuring an empty object.
We try to grab stock off of that empty object it doesn't exist.
So zero is used if I pass in product we are going to see the value up here.
Two hundred and one.
Let's go ahead and just demonstrate that I'll pass in product.
I'll go ahead and rerun the program and I get order.
Red notebook and two hundred and one.
So in this case we pass an object in which means we're not de structuring the empty object ready structuring
the object that was passed in that object does have a stock property.
So the stock default value isn't used.
And two hundred and one ends up printing.
So that's just a little bit of information on a default parameters and how we can use those with D structuring.
Now let's go ahead and get into the problem that we're running into in our program and to do this we
are first going to head back over to the web server directory and start up our application once again.
So for me that's going to be C D dot dot forward slash web server and once we're here we're going to
rerun that node mod command that's no demand source a forward slash app dot and J.
S and we're going to set up our extension list J S and H B S comma separated and then once we run that
we can go ahead and use our application once again.
Right here I refresh things and I'm going to get the updated forecast for Boston.
Here we can see rain and windy starting in the evening and this is updated information.
Now what happens if I don't provide an address where a match can be found.
So if I provide as an example an exclamation mark we can see that we get an error.
This site cannot be reached.
And the problem is that our server actually crashed in the background.
If we go ahead and scroll up what do we get.
We got an error that we've seen before cannot dish structure property latitude of undefined.
So in this case the geo code callback gets executed.
We pass in a value for error and we don't pass in a value for the success data since it was not a success.
We could not find a match.
Error is provided in that case.
Now the problem here is that we're still trying to ditch structure this object to fix that.
All we do is we set up an empty object default value.
Now the program is going to work as expected in both cases.
So I'm going to add that on Save the file and down below we get our node man server restarting I'm gonna
refresh the program and what do I get.
I get my correct Jason response error unable to find location.
Try another search.
Now if I was to swap this out for a good location once again like Philadelphia this time I would still
see what I'm expecting.
My three properties with the forecast showing up.
So by setting up a default object for the object we're trying to D structure we make sure that this
code still works even if no data was provided.
Now you might say.
Doesn't that mean that latitude longitude and location would be undefined and you're right they would
be undefined when I d structure them off of this empty object.
But in that case we know error exists which means the only code that runs is this code and this code
doesn't use those three values.
That's only used down below when things went well.
All right.
That's a quick aside.
Just fixing this little problem in our application.
That's where we're gonna stop for this one.
There will be some challenges throughout the class where will you will have to use the parameter default
Value Syntax.
So let's go ahead and jump back in and actually wire up the front end of our application to use this
endpoint and allow the user to fetch whether from the browser.