Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vilicvane/js-operator-overloading
Overload operators in JavaScript (for fun).
https://github.com/vilicvane/js-operator-overloading
Last synced: about 1 month ago
JSON representation
Overload operators in JavaScript (for fun).
- Host: GitHub
- URL: https://github.com/vilicvane/js-operator-overloading
- Owner: vilicvane
- Created: 2016-02-06T09:31:51.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-06T12:29:55.000Z (almost 9 years ago)
- Last Synced: 2024-11-30T15:41:57.162Z (about 1 month ago)
- Language: JavaScript
- Size: 1000 Bytes
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JavaScript Operator Overloading
This is a demo show case how can we make use of `valudOf` to implement operator overloading in JavaScript.
## vector.js
```js
Vector.createVector('a', 1, 2);
Vector.createVector('b', 4, 6);
Vector.createVector('c');c = a + b;
c = a - b;
c = a * 2;
c = b / 4;
``````sh
node vector.js
```The implementation in `vector.js` accepts two vectors with operators `+` and `-`; or one vector and a scalar with operator `*` and `/`.
## super-vector.js
```js
Vector.createVector('a', 1, 2);
Vector.createVector('b', 4, 6);
Vector.createVector('c', 2, 1);
Vector.createVector('d');d = a + b - c;
d = -a - b + c;
``````sh
node super-vector.js
```The implementation in `super-vector.js` accepts multiple vectors with operators `+` and `-`.
## More Implementations
Instead of using setters, it is also possible to use other way to retrieve the calulation results that help distinguish what operators are used.
For example, you can implement something like this:```js
let a = new Vector(1, 2);
let b = new Vector(2, 3);let c = v(a + b);
let d = v(a * 2);let e = sv(a + b - c - d);
```