Giter VIP home page Giter VIP logo

operator-override's Introduction

๐ŸŽ๏ธ operator-override

It allows JavaScript to support operator override, More elegant override style use decorator.

Use Typescript, can provide friendly code hints.

Tip

Dont't worry about what decorator does in output code, because they only exist compile time โŒš๏ธ. This mean that you can't use decorators other than those in decorators.js, This greatly reduces side effects. If you want use other decorator, you just need @babel/plugin-proposal-decorators๐ŸŽฃ.

Install

Using npm

npm i -D babel-plugin-operator-override

or using yarn

yarn add babel-plugin-operator-override -D

Quick Start

Just add babel-plugin-operator-override in your .babelrc

By the way, the legacy must be a true, because operator-override extends @babel/plugin-syntax-decorators to support decorator syntax.

{
  "plugins": [
    ["babel-plugin-operator-override", {"legacy": true}]
  ]
}

๐ŸŽ‰Now you can start using it!

Usage

operator-override use decorator to support operator override

const {Add, Sub} = require("../dist/decorators.js")

class Point {
	constructor(x, y){
		this.x = x
		this.y = y
	}

	@Add
  	addPoint({x, y}){
    		return new Point(this.x + x, this.y + y)
	}
	
	@Sub
	subPoint({x, y}){
		return new Point(this.x - x, this.y - y)
	}
}

let point1 = new Point(1, 2)
let point2 = new Point(2, 3)

console.info(point1 + point2) // Point { x: 3, y: 5 }
console.info(point1 - point2) // Point { x: -1, y: -1 }

Maybe you perfer this way to override operator, it's allowed

class Point {
	constructor(x, y){
		this.x = x
		this.y = y
	}

	$operatorAdd({x, y}){
    		return new Point(this.x + x, this.y + y)
	}
	
	$operatorSub({x, y}){
		return new Point(this.x - x, this.y - y)
	}
}

let point1 = new Point(1, 2)
let point2 = new Point(2, 3)

console.info(point1 + point2)
console.info(point1 - point2)

operator-override's People

Contributors

dependabot[bot] avatar

Stargazers

injoker avatar liudonghua avatar jerry avatar  avatar MintBlue avatar SADAYZF avatar Ezio Auditore avatar Nate Chen avatar Ash One avatar rengar avatar LeeYaMaster avatar Alexander avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.