Defining Proxies

solve fundamental operations (like property lookup, assignment, enumeration, function invocation, etc.) using proxies

We'll cover the following...

We can create a proxy in the following way:

Press + to interact
let proxy = new Proxy( target, trapObject );

The first argument is target representing the proxied constructor function, class, or object.
The second argument is an object containing traps that are executed once the proxy is used in a specific way.
Let’s put our knowledge into practice by proxying the following target:

Press + to interact
class Student {
constructor(first, last, scores) {
this.firstName = first;
this.lastName = last;
this.testScores = scores;
}
get average() {
let average = this.testScores.reduce(
(a,b) => a + b,
0
) / this.testScores.length;
return average;
}
}
let john = new Student( 'John', 'Dwan', [60, 80, 80] );
console.log(john);

We will now define a proxy on the target object john:

Press + to interact
let johnProxy = new Proxy( john, {
get: function( target, key, context ) {
console.log( `john[${key}] was accessed.` );
// return undefined;
}
});

The ...

Access this course and 1400+ top-rated courses and projects.