所以我有一个简单的的Javascript对象:我可以分配对象操作符吗? “例如+, - ”
function Vector(x, y){
this.x = x;
this.y = y;
this.magnitude = function(){};
this.add = function(vector){};
this.minus = function(vector){};
this.normalise = function(){};
this.dot = function(vector){}
//...
}
我想执行以下操作:
var a = new Vector(1,1);
var b = new Vector(10,5);
var c = a + b
a += c;
// ... and so on
我知道这是可能的为其他语言的对象实施运营商,如果我可以在Javascript中执行,将会非常好用
帮助将非常感激。谢谢! :)
您正在寻找的术语是“过载”。 – 2010-01-24 17:13:11
我会建议你让Vector在执行该操作之前返回一些值并删除'new',所以它就像var a = Vector(1,1); – Reigel 2010-01-24 17:15:52
谢谢Reigel!你能向我展示你的意思吗? :) – RadiantHex 2010-01-24 17:17:05