Changing function scope with jQuery.proxy
Functions that are owned by an object are called methods. This is not a technicality name - methods have owners and their owners are objects. At times you'll end up needing to use a method but will need to change it's scope (what this means to it). For that, we will use the jQuery.proxy() to change (or define) the scope of functions.
Video Transcript
00:00Throughout our courses in our animation training and also our JavaScript training in general
00:05We talk a lot about scope and what scope is and in general this is really what you're asking yourself when you're asking
00:11What is a scope you're asking?
00:13What is the this if you use the word this who does it apply to and this really relates to when you're inside of a function
00:20Functions most of the time are treated as methods and a method means is that the function belongs to a certain object
00:27So whenever we say the word this inside of a function that is a method that this would return that object
00:33What happens if you want to actually change?
00:36What that this is and for that we're going to use the jQuery's
00:40proxy function or method really and use that to
00:44Override and change the default configuration of our function so we could manipulate our
00:48Animation more easily and more quickly and leverage a function that was originally created to be used by a specific object
00:56Let's see what I mean. It's a really great and a really important lesson. So let's write jump right in
01:00It's short and meets with a sample and with it also a wrap up this
01:07Sample really we had one issue that we left behind of
01:11Calling a function that it's this was completely different than what we wanted it to be although we could have done it in the same
01:18Video I thought it's such an important topic that I want to separate it. Don't worry. It's really easy very simple and
01:24Won't take us too long. So if I jump into my application again in my mouse move where I'm calling this
01:30On image update the way I would do that and I'm going to use jQuery for that
01:35I'm basically just going to basically call the global jQuery function by typing jQuery
01:41Now I'm just going to go ahead here and just trace your jQuery
01:45Just so you could see it and the jQuery object is basically an object that exists
01:51Oh, I fruit. I did it actually in after the animation. So we're gonna have to wait a second here and
01:59Reference error traces. Oh, I did it. I'm so silly. I did a trace with it, which is an action script thing
02:05So let me do it. Just go to my console log and I'm gonna grab this jQuery call
02:10And put it right up above right after we are loaded so it's gonna immediately happen
02:16And if I just click on refresh here, we're gonna see the global jQuery static object right away here
02:22You'll use the global jQuery object whenever you're not approaching a specific object and you want to have access to global properties
02:29And this is a really important topic in general because most of our animations so far really in tween light
02:35Were static functions as well and jQuery has static functions as well
02:41And one of them is the proxy function now the proxy function does exactly the
02:46Issue that we had right now that we want to change what this actually means and the way I do it
02:52I just call jQuery proxy and
02:55I basically tell it for the function on anim update call it but when you call it change the context of it
03:03change what this is for it and this for it should be the
03:08Tween pan so I'm literally forcing it to stay in that tween pan
03:13And if I do that and save my application just go and click on refresh
03:17We're gonna see that now once our animation is complete and whenever we rolled over it should still work
03:23It should work because it's gonna send in the right value. So one is we could see that the call is is is
03:31Working they're not getting any error
03:34But we're not actually seeing it
03:37The reason why it's not working is because it is working
03:41What's actually happening here is jQuery is creating a new function that has the scope of tween pan
03:50When we call the function in other words, it's actually creating a function
03:54So we want to actually also trigger the function call out once we create the proxy
03:59Now that we've done that and we go back into our application and click on refresh this time around
04:04not only won't we get any error, but our pan will actually do what we want it to do which is get called and
04:12Refresh the view just the way we wanted it to view. So I think I
04:18think this dog waited for far long enough before it got its treat and I think we should really give him a treat and
04:25I think that's what we're gonna do next when we actually move into
04:30Sing our next problem in our animation because if you look into our animation right now, although it's working really nicely for us right now
04:37It's not really the same animation because we have your three animations and these three animations are not exactly
04:44happening when we're using these rolled over effects and the reason is is because we have it would take us a lot of work to
04:51take into consideration the delays and to take into consideration a lot of tweens and
04:57That's exactly what I want to do in our next chapter because this is gonna be a wrap for this chapter and in the next chapter
05:05We're gonna revisit the exact same sample and recreate it
05:08but instead of recreating it with the
05:12tween light functions
05:13We're gonna move into the tween timeline the timeline light which will enable us to have a lot more control over our animations
05:21But even before we do that in the next lesson
05:24What I want to do is continue looking at a few more controls such as how to play and pause our video
05:29So see you in the next lesson where we continue tweaking our flow before we move on to the next chapter
