A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-proxy-constructor/ below:

JavaScript Proxy() Constructor - GeeksforGeeks

JavaScript Proxy() Constructor

Last Updated : 22 May, 2023

JavaScript proxy() constructor is used to return the proxy constructor function for the object(e.g. property lookup, assignment, enumeration, function invocation, etc). 

Syntax:

let p = new Proxy(target, handler);

Parameter: The proxy object accept two parameters as mentioned above and described below:

Example: 

HTML
const Person = {
    Name: 'John Nash',
    Age: 25
};

const handler = {
    // target represents the Person while prop represents
    // proxy property.
    get: function (target, prop) {
        if (prop === 'FirstName') {
            return target.Name.split(' ')[0];
        }
        if (prop === 'LastName') {
            return target.Name.split(' ').pop();
        }
        else {
            return Reflect.get(target, prop);
        }
    }
};

const proxy1 = new Proxy(Person, handler);

console.log(proxy1);

// Though there is no Property as FirstName and LastName,
// still we get them as if they were property not function.
console.log(proxy1.FirstName);
console.log(proxy1.LastName);    

Output:

[object Object]
John
Nash

Note: The above code can be run directly in terminal provided NodeJs is installed, else run it in a HTML file by pasting the above in script tag and check the output in console of any web browser.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4