一个babel-plugin让javascript支持重载运算符
发布于 7 年前 作者 gzz2000 4169 次浏览 来自 分享

Github

欢迎大家提建议

效果:

class Point {
    x = 0;
    y = 0;

    constructor(_x, _y) {
		if(_x) this.x = _x;
		if(_y) this.y = _y;
    }

    operatorAdd = (b) => {
		const a = this;
		return new Point(a.x + b.x, a.y + b.y);
    }

    operatorMul = (b) => {
		const a = this;
		return new Point(a.x * b, a.y * b);
    }
};

let a = new Point(1, 2), b = new Point(3, 4);

console.log(a + b * 3);
1 回复

JavaScript 元编程 :doge:

回到顶部