codalien / operator-overloading-js

Simple Operator overloading library for JS.
160 stars 26 forks source link

Provide a functional interface which doesn't pollute Function.prototype #9

Closed chocolateboy closed 9 years ago

chocolateboy commented 9 years ago

Kudos for this! It looks like a well-thought-out implementation which avoids the obvious gotchas and is mindful of the scoping/performance issues which plague other implementations[1], but I could never use it in a library or production code as it pollutes a native object (Function), which is considered a no-no in JavaScript.

Have you considered providing a functional interface as well/instead e.g.:

import overload from 'operator-overloading';

overload(() => {
    let c1 = new Complex(1, 2);
    let c2 = new Complex(3, 4);
    assert.equal(c1 + c2, '4+6i');
})();

The method could still be enabled explicitly e.g.:

require('operator-overloading/global');

1 Although, surprisingly, this article suggests the performance hit isn't that large...

kushal-likhi commented 9 years ago

Hey, thanks! looks like a good idea. Agreed. :)