Interfere Constructor calls with ES6 Proxies (#tilPost)

I came along ES6 Proxies and asked myself how to interfere a new call. Here we go!
// proxy handler
const handler = {
// let’s interfere the `new` call
// log out which object was called with which arguments
construct : ( targ…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I came along ES6 Proxies and asked myself how to interfere a new call. Here we go!

// proxy handler
const handler = {
  // let's interfere the `new` call
  // log out which object was called with which arguments
  construct : ( target, args ) => {
    console.log( `Initializing ${ target.name } with:`, args );
    return new target();
  }
};

/**
 * Interfere a constructor function
 */
function ConstructorFunction() {
  this.call = () => {
    console.log( 'method call 1' );
  };
}

const ProxiedConstructorFn = new Proxy( ConstructorFunction, handler );
const foo = new ProxiedConstructorFn( 'foo' );
// logs "Initializing ConstructorFunction", [ "foo" ]
foo.call();
// logs "method call 1"

/**
 * Interfere a class constructor
 */
class ClassConstruct {
  constructor() {}
  
  call() {
    console.log( 'method call 2' );
  }
}

const ProxiedClass = new Proxy( ClassConstruct, handler );
const bar = new ProxiedClass( 'bar' );
// logs "Initializing ClassConstruct", [ "bar" ]
bar.call();
// logs "method call 2"

It's not only get, set and new that can be interfered. You can find a complete list on MDN.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2017-02-22T23:00:00+00:00) Interfere Constructor calls with ES6 Proxies (#tilPost). Retrieved from https://www.scien.cx/2017/02/22/interfere-constructor-calls-with-es6-proxies-tilpost/

MLA
" » Interfere Constructor calls with ES6 Proxies (#tilPost)." Stefan Judis | Sciencx - Wednesday February 22, 2017, https://www.scien.cx/2017/02/22/interfere-constructor-calls-with-es6-proxies-tilpost/
HARVARD
Stefan Judis | Sciencx Wednesday February 22, 2017 » Interfere Constructor calls with ES6 Proxies (#tilPost)., viewed ,<https://www.scien.cx/2017/02/22/interfere-constructor-calls-with-es6-proxies-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Interfere Constructor calls with ES6 Proxies (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/02/22/interfere-constructor-calls-with-es6-proxies-tilpost/
CHICAGO
" » Interfere Constructor calls with ES6 Proxies (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2017/02/22/interfere-constructor-calls-with-es6-proxies-tilpost/
IEEE
" » Interfere Constructor calls with ES6 Proxies (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/02/22/interfere-constructor-calls-with-es6-proxies-tilpost/. [Accessed: ]
rf:citation
» Interfere Constructor calls with ES6 Proxies (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/02/22/interfere-constructor-calls-with-es6-proxies-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.