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

Proxy

The proxy design pattern is when one function/object behaves as if it is another one. There are many usages of it mainly when it comes to security, tracking when we want to have information go through a funnel so we can control it, influence it or monitor it. In javascript the most common implementation of the proxy design pattern is when it comes to keeping scope of functions. In this lecture we will create a proxy fixer for the issues of losing scope in javascript.

Video Transcript

00:01Let's get started with our last structural design pattern,
00:04the structure design pattern named proxy.
00:08So the proxy design pattern is used
00:11when we want to have a function or an object
00:14behave as if it is another one.
00:17Now, before I even continue,
00:19you probably are familiar with the word proxy.
00:21Even if you've never worked with proxies before,
00:24a proxy on a server, what is it?
00:26It's when you're going through a server
00:29that is basically mimicking you
00:32so you're not you directly when you're going to a server,
00:35you're going through a proxy,
00:36and then that proxy could be or to try to anonymize you
00:39so your information is not revealed
00:41or to protect children from going into sites
00:44that they shouldn't be going into
00:46or a million and other one possibilities.
00:49So the idea is to mimic one through another
00:53and the usages of it can be, it could be anything.
00:57It could be to control that user,
00:58to influence that user, or maybe even to monitor it.
01:02So this is if it relates to users,
01:04it relates to objects as well.
01:06In JavaScript, the most common gateway
01:09or the most common proxy you will meet
01:11is when you lose scope in a function.
01:13So we're going to look at that in this lecture.
01:15We're going to see what do you do
01:16when you lose a scope of a function?
01:20How do you actually get that scope back using a proxy?
01:25So let's jump right into it and let's see that in action.
01:30So as we said, a proxy, literally what it does,
01:33it's when one function or objects
01:35behaves as if it's another object.
01:38Now, the most common place you would see that
01:40on the client end is when a function loses its scope
01:43and you want to give it a scope again,
01:45just like we had in our scenario where our shape facade,
01:48where we created a function
01:49just so we could keep reference to our scope.
01:52What I want to do next,
01:52I'm going to show you how we could have done this
01:54in jQuery and it's quite easy.
01:56I would use the dollar sign and write proxy.
01:59And in my proxy, I would first of all send
02:01as the first parameter, the function I want it to trigger.
02:03And again, it's a function once we send it as a function.
02:06And the next parameter would be the scope itself,
02:09making sure that our shape would still keep
02:11that same original scope as it had previously.
02:15And if I go ahead and save this
02:16and run my application one time,
02:18you'll see that my application will,
02:20oh, no, it won't, because I have an error here.
02:23I need to delete that and need to delete that.
02:25Let's go ahead and give it one more try.
02:27And we should see that everything is still working
02:30as it did before, beautiful.
02:33Let's continue down this path.
02:34So now that we know what a proxy is,
02:36and there's definitely proxies
02:38that are more complicated than that,
02:39such as in scenarios where there's a security leak
02:42or security need,
02:43and then we're redirecting everything
02:45through some sort of a security proxy,
02:47like you hear a lot of times on the web
02:49when things are communicating through a third party server
02:52when it's going through a proxy.
02:54That's the same kind of idea
02:55where an object, instead of going directly through itself,
02:58is going through a third party
02:59that is mimicking the behavior of not going through itself.
03:03All right, with that said,
03:04I wanna actually create our own proxy,
03:06and I'm gonna call it binder
03:07because what it does and what it'll do
03:09is bind our functions.
03:10And our binder is gonna take two parameters.
03:12One will be our scope,
03:14and our second is gonna be the function
03:16that we're actually going to trigger.
03:18Now, the logic here is very simple.
03:20All we're really doing is the same type of construct,
03:23same type of logic,
03:24where we're basically just returning the item,
03:27returning that function,
03:28but instead of triggering it directly,
03:30I'm gonna call the apply function
03:32and set my scope as my first parameter
03:35and set my second parameter as the arguments
03:38that are actually being sent inside of this function.
03:41So what I just did right here
03:42is basically recreated all these steps
03:44that I would have done maybe manually or using jQuery
03:47and made myself not use directly jQuery.
03:49And that's all I really had to do here
03:51where now I just have to make sure
03:53that our first parameter will be our shape,
03:56second parameter will be our get ID,
03:58and I'm gonna go ahead here and just copy this,
04:01and I'm gonna paste it right here.
04:03And what I wanna do is I'm just gonna change the method
04:05to be the move, and we could delete all of that,
04:10and I'm gonna go ahead and do the exact same thing here,
04:12and this time around, we wanna have the color,
04:17and let me just delete all this extra goodness,
04:21and it's much cleaner, it's much more simple,
04:24at least to the eye, and we could use this binder
04:27wherever we want to.
04:28Now again, if you know that you're gonna be working
04:29with jQuery, then go ahead and just use that proxy method
04:32and that will do the same exact kind of logic
04:35that you would have there going on.
04:36And if I just go ahead here and click on refresh,
04:38and by the way, there are some versions
04:40that you could do this directly, natively in JavaScript,
04:43but it's not 100% solidified and 100% working.
04:47And I see here, at least it's not supported
04:50in all browsers yet.
04:51So I'm not gonna even talk about it,
04:52but I could see that I still have here
04:54some sort of an error, so let me just double check
04:56and see that everything in here is right.
05:00I'm sending my shape first,
05:01and then I'm sending my function,
05:03and oh, yeah, what I forgot to do
05:05is now that I'm calling this function,
05:07what I really wanna do is I wanna return any value
05:10that comes back, because these values are coming back
05:12and they're trapped inside of here, they're not returned.
05:14So now I've added that return,
05:16and that should sort out that problem,
05:17and it's fine, functions that don't have a return value
05:19would just return nothing,
05:20and that's completely fine as well.
05:22So if I go here and click on refresh now,
05:24everything should be back to working,
05:26and I could also add things dynamically,
05:28and we just finished another section.
05:33In this lecture, we met the proxy design pattern,
05:36and with it, we conclude our structural design patterns.
05:40In the next section, we're gonna move into
05:42a complete new cluster of design patterns,
05:46which are clustered together
05:47and named behavioral design patterns.

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!