Using a function value

Asked

Viewed 73 times

-1

I want to use the value within a function outside of it. I have the global variable, and as it is a variable that is never the same, it presents it. I tested it, and it only worked once with the undifined value, because the function was not executed.

var circle_x;

function deselectElement(evt){
  if(selectedElement != 0){
    selectedElement.parentNode.removeAttributeNS(null, "onmousemove");
    selectedElement.removeAttributeNS(null, "onmouseup");
    selectedElement = 0;
    dx=evt.clientX;
    //objet 1
    if(selectedLineX == 1){

        circle_x=currentX;

    }

  }

}

alert(circle_x);
  • Probably did not work because it is extremely difficult to manage global variables: http://answall.com/a/32252/101. And if it is not possible to get the information you that, ie if you do not enter this if, the function must return what?

  • The function serves to unselect an SVG object. When the cursor is wide.

  • Can you show more of your code? There are variables there that you can’t see where they come from.

  • Did the answer help? Do you think you should accept it?

1 answer

3

Delete the global variable. It probably didn’t work because it’s extremely difficult to manage global variables. See that answer.

function deselectElement(evt){
    if(selectedElement != 0){
        selectedElement.parentNode.removeAttributeNS(null, "onmousemove");
        selectedElement.removeAttributeNS(null, "onmouseup");
        selectedElement = 0;
        dx = evt.clientX;
        //objet 1
        if(selectedLineX == 1){
            circle_x = currentX;
            return circle_x;
        }
    }
    return null; //não me parece coisa boa mas tem que retornar algo, pelo menos assim está explícito    
}

var circle_x = deselectElement(evt);
alert(circle_x);

I put in the Github for future reference.

Here the code does the communication as it should, through the input (parameters) and output (return) of the function.

Note that you continue to have global variables. Where does the currentX? Even the best programmers can’t manage code full of global variables.

And the variable dx is not being used for anything. The code has some weird stuff, but I think the central question is answered. If not, give more details on your question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.