JavaScript proxy()
Posted by Superadmin on May 03 2023 14:23:35

JavaScript proxy()

By Asapu HarikaAsapu Harika
  

JavaScript proxy()

Introduction to JavaScript proxy()

Today, we will discuss on JavaScript Proxy. It is one of the new features introduced in ES6 standards. JavaScript proxy() is an object in JavaScript that will wrap an object or a function to define the custom behavior of fundamental operations. Proxy() will help to create a proxy for another object which can be used to redefine fundamental operations to that object. Operations like read/ write, enumeration, property lookup, function calls, etc.are some of the fundamental operations which can be redefined. We shall face some new terms as we get deeper into the topic, let yourself push forward as this concept JavaScript proxy() is way much simpler and useful. Proxy on wrapping an object or a function is monitored by ‘target’, a new keyword we shall discuss here. Proxy being similar to metaprogramming in other languages as it is independent of wrapped object or function. Before we move into the coding part, there are some terminologies in Syntax which we need to understand,

Syntax:

let proxy = new Proxy(target, handler);

target and handler are the 2 parameters passed to the proxy().

Traps: Even though this parameter is not present in the proxy() syntax, we shall see its use later. These are the functions used to work on Targets.

We will be using Proxy Class from ES6 standard, hence arguments ‘target’ being the wrap object, ‘handler’ will be the function used on ‘target’ and to do some action on ‘target’, we use ‘trap’. If there is a ‘trap’ on the handler, the handler will run and proxy get a chance to handle it else these operations will be performed on ‘target’

How Does JavaScript proxy() Work?

Examples to Implement JavaScript proxy()

Below are the examples of JavaScript proxy():

Example #1

Code:

<!DOCTYPE html>
<html>
<head>
<title>JavaScript proxy()</title>
</head>
<body>
<h2>Here, we will be displaying employeeNames</h2>
<script>
const target = {
employeeName1: "Karthick",
employer: "CPG"
};
const handler = {};
const proxy = new Proxy(target, handler);
document.write("Name of the Employee: " + proxy.employeeName1 + "<br/>");
document.write("Working in: " + proxy.employer);
</script>
</body>
</html>

Output:

JavaScript Proxy Example 1

Explanation: In this example above, we define two properties employeeName1 and employer and a handler with no properties. As there are no properties defined on the handler, proxy will behave like an original target and hence the output.

Example #2

Code:

<!DOCTYPEhtml>
<html>
<head>
<title>JavaScript proxy()</title>
</head>
<body>
<h2>Access data using Proxy and target</h2>
<script>
let target = {};
let proxy = new Proxy(target, {});
proxy.number = 100;
document.write("Using target: " + target.number + "<br/>");
document.write("using proxy: " + proxy.number + "<br/>");
for(let i in proxy) document.write("Looping: " + i + "<br/>");
</script>
</body>
</html>

Output:

Looping Example 2

Explanation: Here, all operations done on proxy are forwarded to target as there are no traps used here. Hence, proxy.number= sets value on target and proxy.number also returns a value from the target. On iterating proxy, target returns as ‘number’. With no traps here, the proxy is a transparent wrapper on target.get proxy.number.

JavaScript Proxy target

Example #3

Code:

<!DOCTYPE html>
<html>
<head>
<title>JavaScript proxy()</title>
</head>
<body>
<h2>Here, we will be displaying student details with handler function</h2>
<script>
const student = {
stuName: 'Renuka',
stuSchool: 'SMHS',
stuAge: 12
};
const handler = {
get(target, property) {
document.write("<br/>" + `details as mentioned ${property}` + "<br/>");
return target[property];
}
};
const proxyStudent = new Proxy(student, handler);
document.write("Name of the student: " + proxyStudent.stuName);
document.write(" is studying in: " + proxyStudent.stuSchool);
document.write(" of age: " + proxyStudent.stuAge);
</script>
</body>
</html>

Output:

Handler Function Example 3

Explanation: Here, the student object has stuName, stuAge, and stuSchool as properties. Proxy is created and passed to the proxy constructor. A handler object with get functionality is created to perform an action on the proxy objects.

Let’s start adding traps,

get() trap: It is fired when the property of the target object is accessed using the proxy object. In our previous examples, the properties of the object are directly accessed by targets.

Here we would set a trap to compute properties of the target object. These properties are properties whose values are calculated based on the properties of existing values.

Likewise, we have set() trap, to control behavior when the target object’s properties are set.

Example #4

Code:

<!DOCTYPE html>
<html>
<head>
<title>JavaScript proxy()</title>
</head>
<body>
<h2>Proxy with trap</h2>
<script>
const policy = {
policyName: 'Karthick',
policyGiver: 'SBI',
policyID: 123456,
}
const handler = {
get(target, property) {
return property === 'policydetails' ?
`${target.policyName} ${target.policyGiver} ${target.policyID}` :
target[property];
}
};
const proxyUser = new Proxy(policy, handler);
document.write(proxyUser.policydetails);
</script>
</body>
</html>

Output:

JavaScript Proxy Example 4

Advantages of JavaScript proxy()

Below are the advantages of JavaScript proxy():

Conclusion

With this, we come to a conclusion of our topic’ JavaScript proxy(), As proxy() provides an interface to control the behavior of any target object using handler and trap, users can balance among simplicity and utility of an object. We have seen how JavaScript proxy() works with few examples illustrated using traps and without traps. You people can still explore the different traps mentioned here. We have seen a few advantages of using JavaScript proxy() with which you will get to know the use of a proxy() in your daily programming.