This course contains good reference material, but does not reflect our current quality standards.

Check out a free preview of the full Organizing JavaScript Functionality course:
The "Public vs Private" Lesson is part of the full, Organizing JavaScript Functionality course featured in this preview video. Here's what you'd learn in this lesson:

Kyle demonstrates the different between publicly accessible methods and privately accessible methods in JavaScript. He also show some variations on how developers declare a public API for a module.

Get Unlimited Access Now

Transcript from the "Public vs Private" Lesson

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

[00:00:03]
>> Kyle Simpson: He was asked what's the difference between something that's public and private. So I rearranged this slightly to show, I now have an 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.

[00:00:33] 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 is declared on line three is an inner function, it's a private function, it's not accessible to the outside world.

[00:00:47] And we're likely gonna make modules that have lots and lots of those and lots and lots of private variables like for example, the name variable that we see from line one, that's a private variable, but we can make other name variables like we could do id and we could assign it a random number is that person's ID.

[00:01:06] 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.

[00:01:22] The only thing that's public is I make a reference to that function on this object that I return 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 clearer, more clear what's happening and it's this.

[00:01:39] 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 public API, it kind of jumps out at me as like, hey, hey, there's a public API here, okay.

[00:02:11] It's just a stylistic choice that I prefer.
>> Kyle Simpson: Just to hammer home this point of 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 upperFirstName 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 upperFirstName without needing to pass anything into it.

[00:02:44] Still gonna work exactly the same way. It's gonna call this private inner function. That thing is gonna 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 that's why we end up printing Kyle in capital letters.

[00:03:05]
>> Kyle Simpson: Okay.
>> Kyle Simpson: Now hopefully, most people have.
>> Kyle Simpson: Somebody's asking about my variable name text formatting. Yeah it's kind of, I have my own sort of weird style that's actually comes from programming prior to moving to JavaScript. So essentially, I camel case anything that is a function and I use whatever the style is called, I never even remember what they call them-

[00:03:40]
>> Speaker 2: Snake case [INAUDIBLE].
>> Kyle Simpson: 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 [INAUDIBLE].
>> Speaker 2: Its smart though.

[00:03:57] It seems like a good style.
>> Kyle Simpson: I got that from way, way back, like C programming, I did that and it's just something, it's just a habit that stuck with me. It's not a particularly idiomatic thing in JavaScript and a lot of people do it, but that's how I came across it.

[00:04:13] [COUGH]
>> Speaker 2: Is JavaScript not probably case sensitive where if you have like up there, HELLO was in capitals, you've used hello with small letters and it's okay, right?
>> Kyle Simpson: It is not. JavaScript is completely case sensitive. All identifiers are case sensitive.
>> Speaker 2: Okay, good enough.
>> Kyle Simpson: And even in this own piece of code, I was a little inconsistent cuz this last name, probably to keep it consistent, I should have called that last name.