Why Proxy is a Gem in JavaScript?

4 Practical Examples help you master this powerful feature in JavaScript

Shuai Li
JavaScript in Plain English

Photo by Christopher Robin Ebbinghaus on Unsplash

What is a Proxy? What exactly does it do? Before explaining this, let’s look at a real-world example.

Each of us has a lot of things to do in our daily life, such as reading emails, receiving express delivery, and so on. There are times when we may feel a little anxious: there are a lot of spam emails on our mailing lists, and it takes a lot of time to sift through them; The delivery received may contain bombs planted by terrorists, threatening our security.

That’s when you might want a loyal housekeeper. You want the housekeeper to help you do the following: Have it check your inbox and remove all spam email before you start reading it; When you receive the package, have it check the package with professional equipment to make sure there is no bomb inside.

In the above example, the housekeeper is our proxy. While we were trying to do something, the housekeeper did something extra for us.

Now let’s go back to JavaScript. We know that JavaScript is an object-oriented programming language, and we can’t write code without objects. But JavaScript objects are always running naked, and you can do anything with them. Many times, this makes our code less secure.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Responses (15)

What are your thoughts?

We know that JavaScript is an object-oriented programming language, and we can’t write code without objects.

I somewhat agree here. JS *can* operate like an OOP language, but it wasn’t created as one, and out of the box, it isn’t one. It’s weakly typed, coercion is bit of a problem, inheritance is still a bit of a mess, and you can certainly write code in…...

--

“Goland Street” not “Golang Street”, is it intentional? ;)

--

if(property = 'location')

and also `if (property = 'postcode')` - shouldn't these be `if (property === '....`

--