codalien / operator-overloading-js Goto Github PK
View Code? Open in Web Editor NEWSimple Operator overloading library for JS.
Simple Operator overloading library for JS.
Do you think we could propose this idea to the TC-39 Proposal ?
While the test passes they are all wrong for the assignment operators.
For example 'should overload += operator'
// test/tests.js #265
r += 33 + 22;
assertEqual(r, '33+33+22');
Should be
r += 33 + 22;
assertEqual(r, '33+22+=33');
as r += 33 + 22;
should be interpretated as r = r.__addAssign(33..__plus(22));
I will be submitting a pull request for solving this after this issue is posted.
Hello. I suggest alternative operator, that does not used in overloading.
c = a $[+] b; //Not overloadable operator, but this is plus
Also, how to overload assign operator?
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...
Do you think it would be better to use Symbol instead of pure named method ? Method is suppose to be unique, so i guess it could be a good idea to use them, it like the iterator Symbol : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator
Hi,
The overload functions don't seem to be apply in all cases, e.g. if operators are used in an if condition, as in
if (a == b) { // ...
To fix this instance I added the following:
case 'IfStatement':
visit(statement.test, index, program);
if (statement.consequent) visit(statement.consequent, index, program);
if (statement.alternate) visit(statement.alternate, index, program);
break;
to the switch under visit.
I have a similar problem with For loop code, the ternary statement. My quick attempts to fix didn't work here.
Is there an alternative way to ensure all possible Javascript operators are overloaded?
Thanks
Declan
Use the Python names for operator overloading (mostly double underscore both at beggining and end of the function) and if possible the same behaviour.
Hi, when I simply include dist/browser/overload.js
of v. 0.5.1
via HTML script tag no overload
function gets added to the global window
object. Though the prototypes of Number
, Function
, Object
ect. all get the double underscore operator methods.
I'd like to use the operator-overloading in my projects organized as AMD modules. To create an AMD module I could wrap the scripts in dist/browser
, but no luck so far.
Version 0.4.4
from Bower Components is still usable in a browser environment, because it is based upon Function.prototype.enableOverloading
and not on exported functions.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.