02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating a private method in JavaScript

Creating a private method in JavaScript

Video Transcript

00:00And let's make it better.
00:01Let's instead of that move this on setup callback to move
00:05into our internally.
00:06So I'm going to add this to it.
00:08And I'm going to grab this function.
00:09And I like putting the prototypes all grouped up
00:12together.
00:13So I'm going to go all the way down here.
00:15And I'm just going to copy our map object itself.
00:21And I am going to set that up to be on sheet callback.
00:25And again, by the way, notice that the name on
00:29is deliberate.
00:30So I know this is a callback function.
00:32It helps me know that it helps me know that I probably don't
00:35want to call it externally.
00:37Now another way for me to solve that problem
00:39is to just add that function as an internal function that
00:42doesn't have an external API.
00:44And let me show you what I mean.
00:46If I'm outside and I'm interacting
00:48with the API of the object, I could
00:50see the on sheet callback.
00:52Now if I wanted that not to be part of the normal flow,
00:56I could have just immediately before I send that callback
01:00to just create the function.
01:02So I could have just came here and copied that name itself
01:06just before I call it right inside of my function.
01:09And this is completely legal in JavaScript.
01:12I could grab that function, create it internally.
01:14Now it doesn't have a public API because it's not
01:17part of the prototype, although it still exists, obviously.
01:21And in JavaScript, there's no such thing
01:22as a private function, not really.
01:25But in our case, because we created the on callback
01:27inside of the method, we know it's only going to be called
01:30once.
01:31We don't have any fear because we really
01:33don't want anyone exposed to it.
01:35We don't want anyone calling it.
01:36Even though it is exposed, we're kind of telling the world,
01:40please ignore it.
01:41So now if we go to our map itself again externally
01:44and try to look for our on sheet callback,
01:47because Dreamweaver is smart, it's
01:49going to still show it to us.
01:51But that basically enables us to hide
01:54the method if we didn't have these smart editors that
01:57actually told us what was actually going on.
02:00And again, if I would run my application with it
02:02as an internal function or as a prototype function,
02:07both of them were a work.
02:08And that's my way of creating a fake private method.
02:12With that said, what if you wanted
02:14to create a really, really private method?
02:17You could make a method that's even more so private
02:20by just creating a function inside of a function.
02:23Now by creating a function inside of a function,
02:25you're basically making it impossible for anyone
02:28to penetrate it besides you.
02:30Now the way to do that is very simple.
02:32All you would want to do is just create a new function.
02:36And you could give it the same name.
02:37That's completely legitimate.
02:39We're just going to create a function without assigning it.
02:41So now we have a real function here.
02:44We would not call this because it's not
02:45part of the this anymore.
02:46Now it's kind of like this invisible function
02:49that only the setup sheet has access to.
02:52And because we're sending it from the setup sheet
02:55to the on sheet callback, now our send has access to it.
02:59But once we're added there, no one else has access to it.
03:02Thus, if I run my application one more time.
03:05And I'll see that everything is still working.
03:08And not only is everything still working,
03:11I will not see it in the list of methods anymore at all.
03:14It's not part of the callbacks at all.
03:16And there's no public API for it, period.
03:19So no one could call this function
03:20besides your internal element that needs to call it.
03:23Which is a very, very powerful way of coding.
03:26Whenever you want to make sure that a function
03:28could just never be called,
03:29besides when you want it to be called.
03:34Because you could then very easily then create
03:36some sort of an API that controls the access
03:38to these privately created functions.
03:41And this is literally the way you would create
03:42a really private function inside a JavaScript.
03:45Now with that said, we definitely need access to this.
03:49This is probably the most critical element of coding
03:52in JavaScript, especially when you're working with objects.
03:55The question is, will the this that will come back
03:57be the this that we hope for?
03:59And probably it will not.
04:01It might.
04:02And let's see what happens.
04:03Let's see if it does come back or not.
04:04So first of all, we see that the function
04:06that calls back in when we set it to this
04:08is returning to us something very interesting
04:10when we set this.
04:11What it's doing is it's returning to us
04:14the actual full function.
04:15So when we're setting this,
04:17it thinks that this is the on callback function
04:20that we've created.
04:21It's not part of the object anymore.
04:23Now, if you wanted to be proud of the object,
04:25which I hope you do, and I hope you're getting
04:28this really advanced part of programming
04:31that we're talking about right now,
04:32your next step will be to try to tell
04:34the function directly, hey buddy,
04:36you are actually part of this object.
04:38Or when I call you, I want you to know
04:40that when I say this, I mean that.
04:43I mean it, all right?
04:44So that's what we're gonna look at at the next video.
04:45So we're stepping closer and closer.
04:48We're building an object.
04:49So before we even do this, let's do a quick summary.
04:52So what we did is we left outside of our objects
04:55only two things.
04:56We left outside the link itself of the document
04:58that we wanna load.
04:59We left outside the position where we wanna start from.
05:03All the rest is done internally.
05:05Now, the reason why all the rest is done internally
05:06is because those are the parameters
05:10that probably we never wanna change.
05:12Once we create this object,
05:13we want this object to remain as it is.
05:15While we don't wanna have internal things
05:18such as specific URLs or specific locations,
05:22just part of our object.
05:23Because if we would do that,
05:24we would have to change the object each time
05:26we wanted to create some sort of a change.
05:28So now that we get that point and we're in that position
05:31and we've created our first private function,
05:33our goal now is to turn that function
05:35into a function that is accessible to us.
05:37And that we will do in our next video.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!