可以在JavaScript中计算像[object] operator [object]这样的对象吗?

时间:2014-08-31 17:06:11

标签: javascript regex math operators

我想创建像点和点的几何对象。矢量 但我更喜欢可以使用数学运算符(+, - ,*)

来计算它

请参阅下面的code

var Point = function(px,py,pz){return {x:px,y:py,z:pz,class:'point'}};
var Vector = function(p,d){
 //p is origin point of vector
 //d can be other Point or distance(not use it here)
 var V = {};
 if(d.class=='point'){
   V.class = 'vector';
   V.origin = p;
   V.x = d.x-p.x,
   V.y = d.y-p.y;
   V.z = d.z-p.z;
   V.magnitude = Math.sqrt(V.x*V.x+V.y*V.y+V.z*V.z);
   //can be add here some other properties and Methods
   //...
 }else{/* some code not required */};
 return V
};
// My question is if there way can be calculate two vectors using Mathematical Operators
var A = Point(2,4,1);  // Point A
var B = Point(3,-2,5); // other Point B
var V1 = Vector(A,B); // 1st Vector
var V2 = Vector(A,Point(0,1,1)); // 2nd Vector
//now when add 1st Vector with 2nd Vector have a result new Vector like a Math
Va = V1 + V2 ; // i know i must add methods for calculate it 
// but I'm ask if can put this Methods as Operator(+,*,^...)

如果不是:使用RegExp进行上述操作的最佳方法是什么 对不起,如果有错误......

0 个答案:

没有答案