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

Fixing a private methods scope

Fixing a private methods scope

Video Transcript

00:00So let's continue.
00:01So you might actually recall that we've done this before,
00:04but we might not or you might not have understood it
00:06in the depths that you probably would wanna understand it.
00:10Now, what we've done in previous videos,
00:12we've actually added a new prototype method
00:14to the function itself.
00:16As crazy as that might sound, might sound.
00:20So let's see how we do that.
00:20First of all, we really want every function
00:22to have the capability to be bindable.
00:25And what that means basically is,
00:27and if you recall when we were talking about call
00:29and apply, we could very easily create a function
00:33when we're actually calling it directly.
00:35We could go to that specific function just before we call it.
00:39So if I go here in line 40, just before we call it,
00:41we could call it, we could call the apply button.
00:46And once we call the apply button,
00:47we could change the scope
00:48by putting a different scope inside, scope.
00:53And then we could send whatever arguments
00:55or parameters that function had.
00:57Now that we need to send to it.
00:58But in our case, because we're calling the send
01:01not actually when we're needing it,
01:02we're just sending it onwards.
01:04We can't actually use this method.
01:06We can't do that because we need to send a function
01:08that was not called yet to be triggered in our send.
01:11Now to do that, our best option
01:14or our best capability would be to go to the function itself
01:17and add a capability that enables it to be bindable.
01:20So to do that, I'm just going to scroll all the way
01:22to the top here just before I start creating
01:24my new custom object.
01:26And I'm going to add a new custom function
01:29to our function object.
01:30So I'm going to go to function.
01:33And in the function itself, I'm going to go to the proper,
01:35proper dot, proper dot, the proper dot, the proper,
01:39the prototype.
01:41I'm going to go to the prototype
01:42and I'm going to add a new bind function to it.
01:44I'm going to call it bind
01:45and I'm going to call that function, well, bind.
01:48And I'm going to send to it a scope.
01:50Now that's all I'm going to send into that specific function.
01:53That specific function then will grab the actual function
01:57that is inside, which we saw that the way to do that
01:59is we grab that function just by asking for this.
02:02Once we ask for this, we're actually asking for the function
02:05that is being binded right now.
02:08Once we got that function
02:10and we got the original function that was being binded,
02:14which was basically whatever it is,
02:16if it's the setup sheet, then it would be the setup sheet.
02:19It's not the bind function itself,
02:21but it's actually the owner function,
02:23the function that is being called.
02:24So when we'll do that, it will be the on sheet callback.
02:28In other words, the function itself
02:29is the function that we want actually.
02:32Now that we got the function that we want as an object,
02:34we could now return a new object.
02:40A new object and that new function actually
02:43is going to be a completely new function
02:45that we're creating on the fly.
02:47By the way, a private function,
02:48but we're sending it over to whoever wants it.
02:50And what are we going to send back?
02:52We're going to send back that function.
02:53What will that function do?
02:55That function will then access the original function
02:58that was there that now doesn't exist anymore,
03:01but we have a reference to it because it's saved
03:03as a variable inside of the function.
03:06So we're accessing that original function
03:08and using the apply method.
03:10And that apply method would apply,
03:12basically would force it to have a scope
03:14of whatever scope we wanted it to be.
03:17And we would take whatever arguments
03:18that were sent inside of this function
03:21and just pass them onwards.
03:22And to do that, there's a secret hidden variable
03:25called arguments that always is created
03:28every time you create a new function.
03:30Every function has an arguments object.
03:33So let me just make sure I type it right,
03:34which is all lowercase, A-R-G-U-M-E-N-T-S.
03:40Now arguments as a whole, all it really is
03:43is a simplified array that contains all the properties
03:46that are sent to a function.
03:48Now, as we learned in the past,
03:49the apply function basically changes the scope
03:53and accepts an array of properties.
03:56And then it calls that specific function.
03:58Now, because once we called the bind function
04:01inside of our own function, once we,
04:03oh, by the way, we have here a little error,
04:05so we probably wanna fix that.
04:06Oops, oh, yeah, yeah, yeah.
04:08That's what I don't like about Dreamweaver,
04:10to be honest right now.
04:11I just don't like this.
04:13I don't like this hinting crap.
04:16Fun, shit, beautiful.
04:19So let's rewrap this again around our head
04:22because this is a very, very complicated topic
04:24and I'm so happy that we have an opportunity
04:25to see it in action.
04:28Our bind, and you know, let's first make it work
04:30and then we'll look at it more deeply
04:32and understand how exactly it works again, okay?
04:34So we created a bind function.
04:36Now that we created that bind function,
04:38we could actually force this on sheet callback to be binded.
04:42All we have to do is call that bind function.
04:47And in that bind function,
04:49all we need to do is send to that bind
04:51the actual this that we want it to be
04:54that in our case it's where we're at.
04:56That's all we have to do for our function to work
04:58and now when we call the log again
05:00and let's test it out before we explain it again
05:02in depth how it works.
05:04So if we go into our Google Maps traveling
05:06and we click on refresh,
05:07this time around the this itself that gets back
05:10should be our map itinerary,
05:12which is exactly what it is, all right?
05:14So let's see why.
05:15First of all,
05:16we're not calling the on sheet callback anymore.
05:18Instead of that, we're calling the function of the function
05:21because every function has functions.
05:22We saw the apply, which we worked with,
05:24we saw the call, which we worked with.
05:26Now, if those aren't really clear to you yet,
05:28please go back to one of our earlier videos
05:31and check it out.
05:32Now, if you don't know where that is,
05:33all you have to do go back to one of our titles,
05:36which I believe it was called and apply
05:37using the JavaScript prototype.
05:39It's the first content lesson inside of that course.
05:44So it's inside of the last course that we were working on.
05:47So basically what we're doing here,
05:49first of all, on the exterior API of it,
05:54we're basically asking the function to change its scope.
05:57That's all we're doing.
05:58Now, how it works inside, let's see how it works inside.
06:01In the bind function itself that now every function
06:03will have because we added this function bind option,
06:07the bind itself accepts the scope.
06:09That's all it takes.
06:10Now, because we're already inside of the function,
06:12we've seen already before.
06:14When we ask for this, we get the function itself.
06:17Now, not really true.
06:19Now that we were already working with objects,
06:22what we actually get is the object.
06:24Now, although we're inside of the bind function,
06:27it belongs to the function object.
06:30Every function is an object as well,
06:31especially after you start building properties
06:33and add things to it.
06:35So if it's a function of a function,
06:37we get that this itself is not gonna be the function,
06:39but it's gonna be the hosting function
06:41or hosting object.
06:43As you can see, even a function is treated as an object
06:46in JavaScript, and there is really no difference
06:48between a function and an object in JavaScript.
06:52So in our scenario, in our case, when we ask for this,
06:55we're not gonna get the bind back.
06:56What we're gonna get is the hosting function.
06:59And in our case, when we look at our example,
07:01that would be our on-sheet callback.
07:03Now that we got our on-sheet callback,
07:05we save that in line 23 so we can use it
07:08when we create this new function and send it out to the user.
07:11Now, although this is an anonymous function,
07:14it will always have access to everything
07:16that's inside of this bigger wrapper function.
07:19So now that we have this internal function
07:21that we're returning to the user, an anonymous function,
07:24this anonymous function once triggered
07:27will then call the function that originally was there
07:30that now is kind of like behind the scenes.
07:33It applies to it the scope that we set to it,
07:36and then applies to it the arguments
07:38that are being sent to this anonymous function.
07:40It's just passing them through
07:42back to that original function that we had.
07:44And that's really the magic key here.
07:46And if you understand this and really understand it well,
07:49and I urge you, if you don't watch this video again,
07:52if you understand every step inside of this
07:54and everything is clear here,
07:55you know object-oriented JavaScript, period.
07:58You might learn more things in the process.
08:00We might teach you more things in the process
08:02as we go on.
08:03But if you get that concept,
08:04you get the most important and most critical thing
08:07you need to understand in how JavaScript works.
08:10All right?
08:11That covers our bind.
08:12Now that we have our application binded
08:14and we have our on-sheet callback
08:16connected to our application,
08:18we could have this response now move on
08:21and integrate more deeply with our application,
08:23making sure that this function is not accessible
08:26unless we want it to be accessible.
08:29All right?
08:31Good.

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!