0

I was looking at this: What characters are valid for JavaScript variable names?

And I had a thought, and I can't decide if it is a good idea or not...

Say you have some object, for example a Vector (x, y, z):

Vector = (function() {
  function Vector(x, y, z) {
    this.x = x != null ? x : 0;
    this.y = y != null ? y : 0;
    this.z = z != null ? z : 0;
    if (isNaN(this.x) || isNaN(this.y) || isNaN(this.z)) {
      throw new Error("Vector contains a NaN");
    }
  }

  return Vector;
})();

and you wanted to add some Vectors, possibly with a function like this:

this.add = function(v) {
  return new Vector(this.x + v.x, this.y + v.y, this.z + v.z);
};

Would it be awful, or awesome, to declare the addition function as this["\u002b"] = function... (or even just this["+"] = function...), and use it like:

var v1 = new Vector(1, 2, 3)
var v2 = new Vector(2, 3, 4) 
var v3 = v1["+"](v2)

Obviously, for "add", theres no actual gain in terms of the code size (I think it might be slighly more readable than v1.add(v2), but something such as "multiply" would be,

var v1 = new Vector(1, 2, 3);
v1["×"](2);

or for other objects something like "integrate" could be:

integrand["∫"](0, 1000)

Thoughts? Is it wonderfully wonderful, or horribly hacky? How reliable are unicode characters across browsers? Would it be safe to use in node.js?

Community
  • 1
  • 1
phenomnomnominal
  • 5,427
  • 1
  • 30
  • 48
  • I would say horrible because unless you are *obfuscating* your code you won't do something like this – Alvin Wong Jun 21 '12 at 03:46
  • It's very hacky and depending on the symbol you chose, you might have to copy and paste it every time. Besides, being contained within the square brackets and double quotes makes it that much more unattractive to the eye. Granted, that it may look slightly better than using a word. If you really want that level of expressiveness, simply roll out your own DSL for whatever domain you're working on. There are plenty of tools to help generate a DSL and its parser fairly quickly nowadays. – Anurag Jun 21 '12 at 03:50
  • I find `v1.add(v2)` easier to read and quicker to type than `v1["+"](v2)`. Also, if you enhanced `.add()` to accept a variable number of vectors I think `v1.add(v2,v3,v4)` would be considerably easier to read than `v1["+"](v2,v3,v4)`. Start using characters that aren't even on a standard keyboard and typing your code would be a real pain. – nnnnnn Jun 21 '12 at 04:37
  • Okay, fair enough, I think I agree, but something like `transform4.multiply(transform3.multiply(transform2.multiply(transform1))` is just awful.. Is `transform4["×"](transform3["×"](transform2["×"](transform1)))` better? But to be honest, I'd probably write it as .multiply and then do a global search and replace. – phenomnomnominal Jun 21 '12 at 04:39
  • And since this project is really CoffeeScript it would be `transform4["×"] transform3["×"] transform2["×"] transform1` If only obj.*unicodeChar* was valid: `transform4.× transform3.× transform2.× transform1` – phenomnomnominal Jun 21 '12 at 05:12

0 Answers0