Javascript:运算符重载 [英] Javascript: operator overloading

查看:156
本文介绍了Javascript:运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用JavaScript了几天了,并且我想要为我定义的对象重载运算符。

I've been working with JavaScript for a few days now and have got to a point where I want to overload operators for my defined objects.

一段时间后在谷歌搜索这个似乎你不能正式这样做,但有一些人声称有一些冗长的方式来执行此操作。

After a stint on google searching for this it seems you can't officially do this, yet there are a few people out there claiming some long-winded way of performing this action.

基本上我已经制作了一个Vector2类,希望能够做到以下几点:

Basically I've made a Vector2 class and want to be able to do the following:

var x = new Vector2(10,10);
var y = new Vector2(10,10);

x += y; //This does not result in x being a vector with 20,20 as its x & y values.

相反,我必须这样做:

var x = new Vector2(10,10);
var y = new Vector2(10,10);

x = x.add(y); //This results in x being a vector with 20,20 as its x & y values. 

我可以采用一种方法来重载Vector2类中的运算符吗?因为这看起来很丑陋。

Is there an approach I can take to overload operators in my Vector2 class? As this just looks plain ugly.

推荐答案

正如您所发现的,JavaScript不支持运算符重载。最接近的是实现 toString (当实例需要被强制为字符串时将被调用)和 valueOf (将被调用以将其强制转换为数字,例如当使用 + 进行添加时,或者在许多情况下使用它进行连接时因为 + 在连接之前尝试添加),这是非常有限的。因此,你不能让你创建一个 Vector2 对象。

As you've found, JavaScript doesn't support operator overloading. The closest you can come is to implement toString (which will get called when the instance needs to be coerced to being a string) and valueOf (which will get called to coerce it to a number, for instance when using + for addition, or in many cases when using it for concatenation because + tries to do addition before concatenation), which is pretty limited. Neither lets you create a Vector2 object as a result.

对于那些想要一个字符串或数字作为结果的人来说(而不是 Vector2 ),这里有 valueOf的例子 toString 。这些示例演示运算符重载,只是利用JavaScript的内置处理转换为原语:

For people coming to this question who want a string or number as a result (instead of a Vector2), though, here are examples of valueOf and toString. These examples do not demonstrate operator overloading, just taking advantage of JavaScript's built-in handling converting to primitives:

此示例将对象的 val 属性的值加倍,以响应被强制到原语,例如通过 +

This example doubles the value of an object's val property in response to being coerced to a primitive, for instance via +:

function Thing(val) {
    this.val = val;
}
Thing.prototype.valueOf = function() {
    // Here I'm just doubling it; you'd actually do your longAdd thing
    return this.val * 2;
};

var a = new Thing(1);
var b = new Thing(2);
console.log(a + b); // 6 (1 * 2 + 2 * 2)

或使用ES2015的

Or with ES2015's class:

class Thing {
    constructor(val) {
      this.val = val;
    }
    valueOf() {
      return this.val * 2;
    }
}

const a = new Thing(1);
const b = new Thing(2);
console.log(a + b); // 6 (1 * 2 + 2 * 2)

或只有对象,没有构造函数:

Or just with objects, no constructors:

var thingPrototype = {
    valueOf: function() {
      return this.val * 2;
    }
};

var a = Object.create(thingPrototype);
a.val = 1;
var b = Object.create(thingPrototype);
b.val = 2;
console.log(a + b); // 6 (1 * 2 + 2 * 2)

此示例转换对象的值 val 响应被强制为原语的属性为大写,例如通过 +

This example converts the value of an object's val property to upper case in response to being coerced to a primitive, for instance via +:

function Thing(val) {
    this.val = val;
}
Thing.prototype.toString = function() {
    return this.val.toUpperCase();
};

var a = new Thing("a");
var b = new Thing("b");
console.log(a + b); // AB

或者使用ES2015的

Or with ES2015's class:

class Thing {
    constructor(val) {
      this.val = val;
    }
    toString() {
      return this.val.toUpperCase();
    }
}

const a = new Thing("a");
const b = new Thing("b");
console.log(a + b); // AB

或只是对象,没有构造函数:

Or just with objects, no constructors:

var thingPrototype = {
    toString: function() {
      return this.val.toUpperCase();
    }
};

var a = Object.create(thingPrototype);
a.val = "a";
var b = Object.create(thingPrototype);
b.val = "b";
console.log(a + b); // AB

这篇关于Javascript:运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆