refresh variables using other function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nibbus
    New Member
    • Nov 2007
    • 18

    refresh variables using other function

    Hello,

    I need a function to alter some variables of other function, how can I do that?

    ex:
    Code:
    function1 ()
    {
    	var a = 7;
    	var b = 25;
    	var c;
    	var d;
    
    	function2 (a, b); //Does a bunch of tests and refreshes c and d values
    	alert(c); // --> should be 2 
    	alert(d); // --> should be 1
    }
    
    function2 (a, b)
    {
    	if (a < 5)
    	{
    	   this.c = 0;
    	}
    	elseif (a > 10)
    	{
    	   this.c = 1;
    	}
    	else
    	{
    	   this.c = 2;
    	}
    
    	if (b < 10)
    	{
    	    this.d = 0;
    	}
    	elseif (b > 20)
    	{
    	   this.d = 1 ;
    	}
    	else
    	{
    	    this.d = 2;
    	}
    }
    Putting c and d as global will solve the problem I think , but I would prefer to have these inside function1.

    I tried var f = new function2(a,b) and then accecing by f.c and f.d but when alerting the value , it comes "undefined"


    Any ideas?

    Thanks in advance,
    Nibbus
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    c and d are private variables. Unless the function is defined within the object function, it can't access them.

    Comment

    • Nibbus
      New Member
      • Nov 2007
      • 18

      #3
      I worked it around by removing var c and d from function1.
      Don´t know if it´s working in the same way, but at least show´s me c and d values using objf2.c and objf2.d ...


      Thank you

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        only for you to mention :) that with that solution you create var c and d implicitly in the global scope ... so after setting them you could access them as global variables ...

        kind regards

        Comment

        Working...