top of page

On the topic of nested functions, I just wanted to mention the fact that when you have nested functions these form what’s known as a closure and we don’t need to know too much about closures. You just need to understand that closure is basically an expression that you know can mix together free variables and something that binds them. In this case, something that closes the expression. And you know put in more simple terms. What that means in this particular case is that the inner functions have you know have access to the arguments and the variables of the outer function but not vice versa. So you can use digital media agency to think of it as a funnel. So if I were to declare a variable here for example or it not even a variable let’s say declare a constant or a let. So let test and let equals.

I am a test so I have declared a. Let’s which I might call the variable here and there but really it’s a lot. I might declare let here and I have access to this anywhere within you know think of like the funnel get anywhere down the line. I have access to this variable that’s left in my squarer I wanted to use that and I want to say console log that out for example test let. Or I could do that and if I run this well it’s not going to work here because I’m trying to console log after I’ve returned. Pretty common scenario. OK, I’ll move it up a line and let’s try that will clear we’ll run right. I’m a test one two three. Fired three times why three times you might ask because I am invoking the function multiple times and then down here I’m just logging out the result. So we see digital media agency have access to this variable. You know this let inside a nested function but it does not go the other way around. And by that token in here I have access to whatever parameters were passed into. So I could do something like this. I could say you know function squarer not take any parameter and instead just take one that was passed in time. No one than in here if I invoke squarer it’s basically doing the exact same thing except I’m being a little bit less reusable in my code. So let’s try that again. I can see that I got the same result that I had before.

Right. Because I’m just directly taking no one from the argument that was passed in. But again it would not go vice versa. No, let me undo that because I kind of preferred it the way it was before. But that’s a main digital media agency that we can definitely do. And you know just to demonstrate that we already know that LET is scoped the blocks if I try to declare let Here let you know. Let me just move my test let fire move that in here. We already know that I won’t have access to this. This outside of this block. Right. So if I try to throw in onto a log test that is going to fail and try to run a test that is not defined. OK, but what about if it’s a car. Remember we mentioned that VAR’s variables are functions scoped. Let’s see what happens Saldivar test let’s see if these works don’t work right. So it’s function scoped but digital media agency itself is a function. So we don’t allow this variable to escape out of this function. It’s confined to this nested function. OK so that’s kind of what we need when talking about closures we close over these things and we can’t access them outside of whatever scope they’re defined in. And you know just for a couple of their examples let’s get rid of that and not let’s rewrite a function here and maybe not use numbers it something just kind of use strings.

Get rid of that. Clear this over here. Let’s say we have a function name formatter takes a first name last name age and we can have a function in here that takes an argument won an argument to a parameter or argument sometimes that can be used interchangeably argument one argument too. And we want to return as a digital media agency that just combines these two together so. Argh argh, one plus arg. Two out of space in between. Right, so we’re kind of just formatting a string here. We return our one and our two and I should probably name named my function as well. So format names. And maybe instead of name formatter, I’ll make it a person formatter for example. And what we’ll do is we will combine first and last name into one right so let’s declare that as a Conte’s formatted name equals format names. And we passed it in first name last name.

At this point, this will be a string. And I kind of like to test it as I.

 

You know what. Let’s return this formatted name and let’s invoke the function by a console log onto a log person formatter Chris Jones and what we can add in a name to or age rather won’t affect anything. Let’s try it out OK. Looks like we were able to format it correctly. It created one long string with space in between. And let’s say OK instead of returning that let us run another function that’s nested in here. And here we can do a function. Let’s get this one to format our data. So let’s say we want to create a digital media agency out of this personal matter with you know key-value associations you know full name equals formatted name age equals whatever age was passed in. So let’s just call this data format or and it will take in a full name and a number already. And what we can do is we can kind of construct an object here as well. So we could say Konst formatted data is an object.

bottom of page