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?