O'Reilly logo

JavaScript Recipes: A Problem-Solution Approach by Keith Cirkel, Russ Ferguson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Russ Ferguson and Keith Cirkel 2017
Russ Ferguson and Keith CirkelJavaScript Recipes10.1007/978-1-4302-6107-0_16

16. Working with Proxies

Russ Ferguson and Keith Cirkel2
(1)
Ocean, New Jersey, USA
(2)
London, UK
 

When Should You Use a Proxy?

Problem

What is a proxy and when should you use one?

Solution

A proxy object allows you to customize the behavior of an object. Some of the ways you can use a proxy is for interception of an object, object virtualization, profiling, and contracts for object use.

The Code

var handler = {
        set (target, key, value){
                  console.info(`property "${key} set on object "${target}" with a value of "${value}"`);
        }
}
var target = {};
var proxy = new Proxy(target, handler);
proxy.a = 'a' //outputs  property ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required