Case insensitive functions in Javascript using Proxy

Demo

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) {
    this.name = name || null;
    this.health = health || null;
    this.alive = true;
  }

  setname(name) {
    this.name = name;
  }

  sethealth(health) {
    this.health = health;
  }

  hit(damage) {
    if (this.health > damage) {
      this.setHealth(this.health - damage);
      console.log('Hit! Health now at ' + this.health)
    } else {
      this.alive = false;
      this.health = 0;
      console.log(this.name + ' 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.

Usage

var proxy = new MonsterProxy('', 0); 
console.log(proxy); 
proxy.SETNAME('Godzilla'); 
proxy.setHEALTH(80); 
proxy.hit(10);
proxy.Hit(10);
proxy.hIt(10);
proxy.hiT(10);
proxy.HiT(10);
proxy.HIt(10);
proxy.hIT(10);
proxy.HIT(10);
console.log(proxy);

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