(mellow jazz music) - It was asked, "What's the difference "between something that's public and private?" So I rearranged this slightly to show I now have and inner function here which I call, upper, and it does the task of taking some variable and upper casing it. And then I have this function here. And I'm using that inner function, but you'll notice that I'm not exposing that upper to the outside world.
That is an internal implementation detail that from the outside world you have no access to, but I'm still able to use it. So this is what we mean, this function that's declared on line three is an inner function. It's a private function. It's not accessible to the outside world. And you're likely going to make modules that have lots and lots of those, and lots and lots of private variables. For example, the name variable that we see from line one, that's a private variable. But we can make other name variables. We could do ID, and we could assign it a random number as that person's ID, alright? So now I have a user ID that is only accessible on the inside.
And the only way somebody can ever see that value is if I choose to make a method that exposes that in some way. Otherwise, it stays private. So ID now, and upper, and name, those are all private things. The only thing that's public is I make a reference to that function on this object that I returned back, that object being a public API. There's a slight variation of lines 13 through 15 that I personally do in my own code to make it slightly more clear what's happening, and it's this. Instead of returning that object, I call it public API, and then I return public API.
Now functionally, that's exactly the same as the previous, but this is slightly more obvious now what this object is. It's a public API. So if I'm looking through hundreds of lines of code, and I see a thing declared called public API, kind of jumps out at me as like, hey, hey, there's a public API here, okay? It's just a stylistic choice that I prefer. Just to hammer home this point to the fact that this inner function still has access to stuff, I don't even have to use it in such a way where I'm passing something to it.
Upper could be called upper first name, and it could be set to automatically use name. And so here, I don't call upper and pass in name, I just call upper first name without needing to pass anything into it. Still going to work exactly the same way. It's going to call this private inner function. That thing is going to have access to this variable through the closure, and it'll be able to receive that value and do something with it, or use that value to do something.
So essentially, I camel case anything that is a function, and I use whatever this style's called. I never even remember what they call it, but whatever-- - Snake case. - Snake case, there you go. I never remember. - That's what it's called. - I use snake case for variables, and I use camel case for functions. And that's just so I remember in my own code, is this something I can call or not? That's not a terribly common style that people use, but that's my own weird style. - It's smart, though. That seems like a good style.
- Yeah, even in this own piece of code, I was a little inconsistent, 'cause this last name, probably to keep consistent, I probably should've called it last name.
Note: This course was created on 03/29/2016 by Frontend Masters. We are pleased to host this content in our library.
- Carousels, panes, and modules
- Middle-end architecture
- Secure phrase generator
- Routing functions
- Calling the API
- Rendering on the page
- Shared data validation