1 00:00:00,240 --> 00:00:06,150 In this video I want to take a few minutes to explore another iOS 6 feature we're going to end up using 2 00:00:06,150 --> 00:00:09,430 to fix a problem with our current application. 3 00:00:09,450 --> 00:00:16,050 This is the default parameters syntax and that's going to allow us to set a default value for a function 4 00:00:16,050 --> 00:00:19,040 parameter should no argument be passed in. 5 00:00:19,470 --> 00:00:22,580 Now we'll explore this in isolation in the playground folder. 6 00:00:22,680 --> 00:00:27,190 Then we'll integrate it into the application to fix a problem with our code. 7 00:00:27,300 --> 00:00:30,960 So to kick things off we have one through six already in here. 8 00:00:30,960 --> 00:00:35,430 I'm going to create a new script seven and I'll call this something like. 9 00:00:35,430 --> 00:00:38,420 Default parameter dot J S. 10 00:00:38,510 --> 00:00:44,160 And in here we're gonna start with probably the most basic function example you'll ever see. 11 00:00:44,170 --> 00:00:48,300 Then we'll explore how we can improve it with default parameter values. 12 00:00:48,300 --> 00:00:54,690 Let's go ahead and create a greeter function over going to do is accept a name as the first and only 13 00:00:54,690 --> 00:00:59,580 parameter and then we're going to use it as part of a message we print down below. 14 00:00:59,580 --> 00:01:06,660 So something like console dialog Hello space followed by the person's name so I'll concatenate on using 15 00:01:06,660 --> 00:01:10,910 the plus operator the values stored in the name variable. 16 00:01:10,920 --> 00:01:16,470 Now if we go ahead and call this we know exactly what's gonna happen and down below I'll do just that. 17 00:01:16,620 --> 00:01:20,460 I'm going to call a greeter passing in a value for name. 18 00:01:20,670 --> 00:01:22,410 I'll go ahead and use my name. 19 00:01:22,470 --> 00:01:28,380 Andrew now from here we want to run the script so down below I am going to use controls C to shut down 20 00:01:28,380 --> 00:01:34,560 node mine and we're gonna switch from the web server folder to the playground directory so S.D. dot 21 00:01:34,560 --> 00:01:39,750 dot forward slash playground and once I'm inside of here I'm just gonna run the script. 22 00:01:39,750 --> 00:01:45,890 So that would be node space then 7 default parameters dot J S. 23 00:01:46,030 --> 00:01:48,610 Now when I run it I'm gonna see Hello andrew print. 24 00:01:48,660 --> 00:01:50,490 No surprise there. 25 00:01:50,490 --> 00:01:56,620 What I'm interested in though is what happens when I don't pass in an argument value to greeter. 26 00:01:56,640 --> 00:02:00,930 So right here all I'm gonna do is call greeter with no arguments. 27 00:02:00,930 --> 00:02:04,830 Now I'm gonna run the code once again and we're not going to get a javascript error. 28 00:02:04,830 --> 00:02:11,060 The program isn't going to crash but the result we do get is just as undesirable right here. 29 00:02:11,070 --> 00:02:12,200 When I run it we get. 30 00:02:12,200 --> 00:02:14,010 Hello undefined. 31 00:02:14,010 --> 00:02:18,060 So when the name is provided things go well when no name is provided. 32 00:02:18,060 --> 00:02:22,090 Obviously we can't use it but we could definitely do better than this. 33 00:02:22,200 --> 00:02:29,010 Instead of something like Hello undefined we could print something like Hello user or hello anonymous. 34 00:02:29,010 --> 00:02:35,200 Now the reason we're seeing undefined here is because that is the default value for a function parameter. 35 00:02:35,220 --> 00:02:38,300 If no argument is passed in none is provided here. 36 00:02:38,310 --> 00:02:43,240 So name is undefined and I see undefined in that message. 37 00:02:43,240 --> 00:02:48,630 Now obviously if we wanted to address this we could put an if statement inside of the greeter function 38 00:02:48,630 --> 00:02:50,520 to check if the name exists. 39 00:02:50,580 --> 00:02:52,740 If it does use it if it doesn't. 40 00:02:52,740 --> 00:02:58,170 Inside of an else clause we could print a completely different message but we can also use the default 41 00:02:58,200 --> 00:03:05,580 parameters syntax to achieve these same results with a lot less code and to set up a default value for 42 00:03:05,580 --> 00:03:07,560 a function parameter like name. 43 00:03:07,560 --> 00:03:10,960 All we do is use the equals sign right after it. 44 00:03:10,980 --> 00:03:16,650 So for the moment let's go ahead and add a second argument into the mix just so it's easy to visualize. 45 00:03:16,650 --> 00:03:19,290 So we take a name and we take an age. 46 00:03:19,290 --> 00:03:22,120 The goal is to set a default value for a name. 47 00:03:22,140 --> 00:03:23,100 What do we do. 48 00:03:23,100 --> 00:03:24,840 We go ahead and we use equals. 49 00:03:24,840 --> 00:03:28,730 Right after it followed by the default value we'd like to use. 50 00:03:28,740 --> 00:03:29,990 This could be anything. 51 00:03:30,030 --> 00:03:35,880 It could be a function an array an object a number a boolean or in our case a string. 52 00:03:35,880 --> 00:03:38,860 Now what name do we want to use by default. 53 00:03:38,880 --> 00:03:44,730 We could just use something generic like a user or anonymous or Hello person whatever you wanted to 54 00:03:44,730 --> 00:03:47,520 use as that default string. 55 00:03:47,520 --> 00:03:53,490 Now if I save things we can see how the program is going to function if an argument is provided that 56 00:03:53,490 --> 00:03:56,490 value will be used which means we'll still see. 57 00:03:56,490 --> 00:03:57,810 Hello Andrew. 58 00:03:57,810 --> 00:04:04,170 If no argument is provided then the default value will be used which means for the second printing we 59 00:04:04,170 --> 00:04:04,760 should see. 60 00:04:04,770 --> 00:04:05,430 Hello. 61 00:04:05,430 --> 00:04:07,140 User down below. 62 00:04:07,140 --> 00:04:09,230 I'm going to go ahead and rerun the program. 63 00:04:09,240 --> 00:04:15,960 After saving my file and that is exactly what I get I get Hello Andrew and Andrew is provided and I 64 00:04:15,960 --> 00:04:19,280 get Hello user when no name value was provided. 65 00:04:19,410 --> 00:04:24,930 Using this syntax makes it really easy to setup default values for your function parameters. 66 00:04:24,930 --> 00:04:28,750 There's one more thing I want to talk about before we jump back into apt. 67 00:04:28,770 --> 00:04:33,960 Jay asked and explore the issue we're going to address and what I want to talk about relates to dish 68 00:04:33,960 --> 00:04:34,710 structuring. 69 00:04:34,920 --> 00:04:40,650 So let's take a quick moment to open up that other playground file we created which was number five. 70 00:04:40,650 --> 00:04:47,850 Yes six objects in here we explored how we could use the object property shorthand and we explored how 71 00:04:47,850 --> 00:04:55,140 we could use object destruction bring we d structured as a standalone statement and we d structured 72 00:04:55,260 --> 00:05:01,470 function arguments like we saw with our transaction function down below now in this case we were dis 73 00:05:01,470 --> 00:05:03,800 structuring the second argument. 74 00:05:03,830 --> 00:05:06,430 So I was expecting it to be an object. 75 00:05:06,530 --> 00:05:10,030 Now currently it is an object we're passing in product. 76 00:05:10,130 --> 00:05:16,720 But what would happen if I didn't pass in any value we know the default value would be undefined. 77 00:05:16,820 --> 00:05:20,090 That's the value used if you don't pass anything in. 78 00:05:20,120 --> 00:05:25,940 So what happened when we try to ditch structure undefined right here we can see what's going to happen 79 00:05:25,940 --> 00:05:27,630 by running this script. 80 00:05:27,680 --> 00:05:33,430 So I've removed that second argument in the call to transaction and I'll run node 5. 81 00:05:33,440 --> 00:05:35,880 Yes six objects dot J. 82 00:05:35,890 --> 00:05:39,530 S and when I do I get a javascript error. 83 00:05:39,530 --> 00:05:46,250 Now the actual error message is cannot dish structure property label of undefined. 84 00:05:46,250 --> 00:05:52,940 So what it's talking about here is this line that is line 32 which it mentions right here. 85 00:05:52,940 --> 00:06:00,650 This is where we try to dish structure a parameter no value is passed in so undefined is used and we 86 00:06:00,650 --> 00:06:03,450 cannot dish structure undefined. 87 00:06:03,530 --> 00:06:08,720 Now we can easily address this by setting up a default parameter value. 88 00:06:08,750 --> 00:06:14,990 So here we are de structuring undefined because no object was provided if we wanted to fix this we could 89 00:06:14,990 --> 00:06:19,760 set a default value for that parameter equal to an empty object. 90 00:06:19,760 --> 00:06:26,270 Now we're always going to get an object whether it's passed in or whether the default object is used. 91 00:06:26,270 --> 00:06:32,690 Now let's run the program one more time to see what we get on line 33 when we log all three variables 92 00:06:32,780 --> 00:06:34,180 out down below. 93 00:06:34,460 --> 00:06:40,280 I'm gonna use the up arrow key and enter to rerun the previous command and what do I get for a name. 94 00:06:40,280 --> 00:06:44,640 I get Andrew for age I get twenty seven and location Philadelphia. 95 00:06:44,660 --> 00:06:48,730 That's coming from the first example for this console log. 96 00:06:48,860 --> 00:06:57,320 I get the type value of order for a label I get undefined end for stock I get undefined as well so here 97 00:06:57,350 --> 00:07:02,400 label and stock are both undefined because no object was provided. 98 00:07:02,510 --> 00:07:08,210 So we d structure to the empty object and this object has no label or stock property. 99 00:07:08,210 --> 00:07:10,160 So we see undefined for both. 100 00:07:10,670 --> 00:07:17,270 So by setting up this default object we can make sure that when we d structure and object the code works 101 00:07:17,420 --> 00:07:20,420 whether or not an object is ever passed in. 102 00:07:20,510 --> 00:07:24,410 Now from here you could take things even further when you're D structuring. 103 00:07:24,410 --> 00:07:29,780 You can also set up default values to use if you don't find a match. 104 00:07:29,780 --> 00:07:37,460 So in this case let's say that for a stock I want to use zero if no stock property exists on the object 105 00:07:37,520 --> 00:07:41,090 that's passed in like I would for a parameter. 106 00:07:41,420 --> 00:07:44,460 I just set up equals afterwords and I pick a value. 107 00:07:44,480 --> 00:07:46,730 Let's go ahead and use zero. 108 00:07:46,850 --> 00:07:53,120 Now if we rerun the program we're going to see that order still prints for type undefined still prints 109 00:07:53,120 --> 00:07:56,570 for a label and now stock is zero. 110 00:07:56,570 --> 00:08:00,650 Now once again we're only seeing zero because no object is provided. 111 00:08:00,680 --> 00:08:03,260 So we're de structuring an empty object. 112 00:08:03,260 --> 00:08:06,830 We try to grab stock off of that empty object it doesn't exist. 113 00:08:06,830 --> 00:08:12,080 So zero is used if I pass in product we are going to see the value up here. 114 00:08:12,080 --> 00:08:13,350 Two hundred and one. 115 00:08:13,460 --> 00:08:17,540 Let's go ahead and just demonstrate that I'll pass in product. 116 00:08:17,540 --> 00:08:20,990 I'll go ahead and rerun the program and I get order. 117 00:08:20,990 --> 00:08:23,230 Red notebook and two hundred and one. 118 00:08:23,240 --> 00:08:29,450 So in this case we pass an object in which means we're not de structuring the empty object ready structuring 119 00:08:29,450 --> 00:08:34,960 the object that was passed in that object does have a stock property. 120 00:08:34,970 --> 00:08:38,150 So the stock default value isn't used. 121 00:08:38,150 --> 00:08:40,960 And two hundred and one ends up printing. 122 00:08:41,030 --> 00:08:47,530 So that's just a little bit of information on a default parameters and how we can use those with D structuring. 123 00:08:47,540 --> 00:08:52,580 Now let's go ahead and get into the problem that we're running into in our program and to do this we 124 00:08:52,580 --> 00:08:59,120 are first going to head back over to the web server directory and start up our application once again. 125 00:08:59,120 --> 00:09:05,330 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 126 00:09:05,390 --> 00:09:11,680 rerun that node mod command that's no demand source a forward slash app dot and J. 127 00:09:11,680 --> 00:09:18,860 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 128 00:09:18,860 --> 00:09:21,580 we can go ahead and use our application once again. 129 00:09:21,590 --> 00:09:26,610 Right here I refresh things and I'm going to get the updated forecast for Boston. 130 00:09:26,630 --> 00:09:32,300 Here we can see rain and windy starting in the evening and this is updated information. 131 00:09:32,330 --> 00:09:37,670 Now what happens if I don't provide an address where a match can be found. 132 00:09:37,670 --> 00:09:42,470 So if I provide as an example an exclamation mark we can see that we get an error. 133 00:09:42,470 --> 00:09:44,510 This site cannot be reached. 134 00:09:44,510 --> 00:09:49,280 And the problem is that our server actually crashed in the background. 135 00:09:49,340 --> 00:09:51,490 If we go ahead and scroll up what do we get. 136 00:09:51,500 --> 00:09:58,470 We got an error that we've seen before cannot dish structure property latitude of undefined. 137 00:09:58,490 --> 00:10:02,240 So in this case the geo code callback gets executed. 138 00:10:02,340 --> 00:10:09,380 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. 139 00:10:09,390 --> 00:10:11,320 We could not find a match. 140 00:10:11,340 --> 00:10:14,200 Error is provided in that case. 141 00:10:14,250 --> 00:10:19,260 Now the problem here is that we're still trying to ditch structure this object to fix that. 142 00:10:19,260 --> 00:10:23,880 All we do is we set up an empty object default value. 143 00:10:23,910 --> 00:10:27,990 Now the program is going to work as expected in both cases. 144 00:10:27,990 --> 00:10:34,670 So I'm going to add that on Save the file and down below we get our node man server restarting I'm gonna 145 00:10:34,680 --> 00:10:36,740 refresh the program and what do I get. 146 00:10:36,780 --> 00:10:41,530 I get my correct Jason response error unable to find location. 147 00:10:41,520 --> 00:10:43,520 Try another search. 148 00:10:43,590 --> 00:10:51,660 Now if I was to swap this out for a good location once again like Philadelphia this time I would still 149 00:10:51,660 --> 00:10:53,130 see what I'm expecting. 150 00:10:53,130 --> 00:10:56,250 My three properties with the forecast showing up. 151 00:10:56,250 --> 00:11:02,490 So by setting up a default object for the object we're trying to D structure we make sure that this 152 00:11:02,490 --> 00:11:07,170 code still works even if no data was provided. 153 00:11:07,170 --> 00:11:07,940 Now you might say. 154 00:11:07,940 --> 00:11:13,620 Doesn't that mean that latitude longitude and location would be undefined and you're right they would 155 00:11:13,620 --> 00:11:17,910 be undefined when I d structure them off of this empty object. 156 00:11:17,910 --> 00:11:23,220 But in that case we know error exists which means the only code that runs is this code and this code 157 00:11:23,280 --> 00:11:25,230 doesn't use those three values. 158 00:11:25,260 --> 00:11:28,330 That's only used down below when things went well. 159 00:11:28,530 --> 00:11:28,910 All right. 160 00:11:28,920 --> 00:11:29,930 That's a quick aside. 161 00:11:29,940 --> 00:11:32,850 Just fixing this little problem in our application. 162 00:11:32,970 --> 00:11:34,700 That's where we're gonna stop for this one. 163 00:11:34,710 --> 00:11:40,500 There will be some challenges throughout the class where will you will have to use the parameter default 164 00:11:40,500 --> 00:11:42,030 Value Syntax. 165 00:11:42,030 --> 00:11:48,090 So let's go ahead and jump back in and actually wire up the front end of our application to use this 166 00:11:48,150 --> 00:11:51,750 endpoint and allow the user to fetch whether from the browser.