Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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')
```