1

Sorry for noobie question,but:

I just recently found out how the '=' operator doesn't just deal with objects/etc. values, but also references. This is cool and all, but I thought that that's not the same for variables, and that it wouldn't create references between variables that store integers or floating numbers. I later figured that I'm correct but here's why I doubted that:

I had a function that had for input four values (fx,fy,sx,sy) , defining two points, and it would return an angle between the line segment from those two points and a ray from the first point (fx,fy) to (+Infinity , fy ). Than I had an event listener for mouse move, and two values fa and sa. My code was:

/*mx and my are the points of the mouse, px and py are x and y of some point*/
sa=fa;
fa=angle(px,py,mx,my);
/*here on I use those values sa and fa for something*/

But the problem was, both fa and sa always had the same value (which was logically confusing for me). I kinda guess that maybe the function returns an object instead of a variable, but I'm not sure. Therefore I thought I should include the function in here:

function angle(fx,fy,sx,sy){
    this.arsn=0.00000000000001;
    this.ba;this.ba1;this.ba2;
    this.d=((fx-sx)**2+(fy-sy)**2)**(1/2);
    this.a = sx-fx;
    this.b = sy-fy;
    this.ba1=Math.asin(b/d);
    this.ba2=Math.acos(a/d);
    if(Math.abs(this.ba1-this.ba2)<this.arsn)
        this.ba=this.ba1;
    else if(Math.abs(this.ba1+this.ba2-Math.PI)<this.arsn){
        this.ba=this.ba2;
    }else if(Math.abs(this.ba1+this.ba2)<this.arsn){
        this.ba=2*Math.PI-ba2;
    }else if(Math.abs(-this.ba1+this.ba2-Math.PI)<this.arsn){
        this.ba=Math.PI-this.ba1;
    }
    return this.ba;
}

Can someone tell me how to not create a reference between fa and sa but still pass the value from fa to sa? I did some research that led me here , but those answers seem way too complicated and are mostly object oriented or use json. All I know is javascript (and I wouldn't say I'm too good at it).

1 Answers1

1

Use var instead of this. when using functions to return values.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47