如何在JavaScript中重新定义数组上的+运算符? [英] How to redefine the + operator on Arrays in JavaScript?

查看:81
本文介绍了如何在JavaScript中重新定义数组上的+运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设使用JavaScript Array 将点表示为 [x,y] ,我如何定义 + 运算符,其点如下:

Assuming points are represented using JavaScript Array as [x,y], how could I define the + operator on points such that:

[1,2] + [5,10] == [6,12]


推荐答案

JavaScript

JavaScript does not have a facility for overriding the built-in arithmetic operators.

您可以通过覆盖 .valueOf()来获取一些有限的技巧。 .toString()方法,但我无法想象您将如何完成所要问的事情。

There are some limited tricks you can pull by overriding the .valueOf() and .toString() methods, but I can't imagine how you could do what you're asking.

您当然可以编写一个函数来实现它。

You could of course write a function to do it.

这篇关于如何在JavaScript中重新定义数组上的+运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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