JavaScript and Maps (in that order)

Proxies in ES6 something.*()

I had an interesting problem today, it involved catiline, my library for web workers, how do I fake console in all it’s various forms in the worker.  This is easy for log, warn etc it just sends back the method names but there are quite a few and they aren’t universally supported in different browsers. In the end I just hard coded a list of the ones I’d go with, but what I really wanted to do was be able create something that allows me to do console.* = function…

As it turns out you can do this with proxies in ES6, and that’s just the beginning. This

A couple notes: that will only work in firefox as V8 has an older version of proxy, (edit this shims new proxy via the old proxy) also I seem to remember you don’t need to use the new keyword for proxies but firefox throws an error if you omit it.

The way it works is that we are intercepting the call to look up the key and then building the function for that key, there is no reason we couldn’t return a value in certain cases. Like with

which makes the method lowercase and tries to return a value if its all caps. Now all of this only uses the ‘get’ option, the full list is here

If you’re into Aspect Oriented Programing then this is going to be your dream come true as you can intercept function calls before they touch the function, like in this example where we wrap it in a try statement to make sure it runs.

Some more links: