Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rob-blackbourn/example-operator-overloading
An example using @jetblack/operator-overloading
https://github.com/rob-blackbourn/example-operator-overloading
Last synced: about 1 month ago
JSON representation
An example using @jetblack/operator-overloading
- Host: GitHub
- URL: https://github.com/rob-blackbourn/example-operator-overloading
- Owner: rob-blackbourn
- Created: 2020-07-04T13:32:11.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-05T04:02:43.000Z (over 1 year ago)
- Last Synced: 2024-03-15T00:56:04.240Z (8 months ago)
- Language: JavaScript
- Size: 345 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# example-operator-overloading
This project demonstrates how to use [@jetblack/operator-overloading](https://github.com/rob-blackbourn/jetblack-operator-overloading).
## Usage
```bash
npm install
npm start
```## Installation
You can add the plugin to your project by adding the npm package.
```bash
npm install --save-dev @jetblack/operator-overloading
```The babel file will look something like this.
```json
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
],
"plugins": ["module:@jetblack/operator-overloading"]
}
```## The code
Here is the code. We need to enable the overloading with the `'operator-overloading enabled'`
statement.```javascript
'operator-overloading enabled'class Point {
constructor(x, y) {
this.x = x
this.y = y
}
[Symbol.for('+')](other) {
const x = this.x + other.x
const y = this.y + other.y
return new Point(x, y)
}
}// Check primitives still work
const i1 = 3
const i2 = 4
const i3 = i1 + i2
console.log(i3)// Check overloads work
const p1 = new Point(5, 5)
const p2 = new Point(2, 3)
const p3 = p1 + p2
console.log(p3)// What happens with nulls?
const s1 = null
const s2 = 'hello'
const s3 = s1 + s2
console.log(s3)console.log('Done')
```