Case insensitive functions in Javascript using Proxy


During research on Microsoft's JScript, I realized that it allows you to call case insensitive functions, while Javascript is supposed to be strictly case sensitivity. I wondered how to emulate this behaviour with normal Javascript, and found out about the ES6 Proxy object. This object allows us to catch and modify fundamental operations, such as property lookups. For normal property lookups this can also be partially achieved with Object.defineProperty, but the Proxy object is much more versatile.

Note: I can think of no realistic use-cases why you would want to have case-insensitive functions in Javascript. It can lead to strange bugs and, as of now, only works in certain browsers. It is also not possible to use with Babel. And although I didn't test it, I doubt minifiers handle this very well. In general, you will want to avoid using this code, is what I'm saying.

JScript examples

In JScript, the following function calls are all valid calls:

WScript.echo('Hello world');
WScript.ECHO('Hello world');
WScript.eCHo('Hello world');
WScript.EcHo('Hello world');

Javascript's equivalent

In order to do this in Javascript, we will first create a class with some lowercase functions, using the ES6 class method:

class Monster {
  constructor(name, health) { = name || null; = health || null;
    this.alive = true;

  setname(name) { = name;

  sethealth(health) { = health;

  hit(damage) {
    if ( > damage) {
      this.setHealth( - damage);
      console.log('Hit! Health now at ' +
    } else {
      this.alive = false; = 0;
      console.log( + ' has died!');

Now the real magic happens:

class MonsterProxy {
  constructor() {
    return new Proxy(new Monster(...arguments), {
      get(target, propKey, receiver) {
        return target[propKey.toLowerCase()];

Proxy takes 2 arguments: the Class I want to apply it to, and the getter that intercepts calls. The last part is again similar to how Object.defineProperty works, except the getter takes different arguments (the Class, the called key and the Proxy object). The code is then wrapped in a class so it is possible to instantiate it with arguments, like you would if the Proxy weren't there.


var proxy = new MonsterProxy('', 0); 

As you can see, every variant of case-insensitivity for the function .hit() is called and valid. The Proxy handler turns everything lowercase and calls that function.

See the example in action on JSfiddle