By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,622 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

refresh variables using other function

P: 18
Hello,

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

ex:
Expand|Select|Wrap|Line Numbers
  1. function1 ()
  2. {
  3.     var a = 7;
  4.     var b = 25;
  5.     var c;
  6.     var d;
  7.  
  8.     function2 (a, b); //Does a bunch of tests and refreshes c and d values
  9.     alert(c); // --> should be 2 
  10.     alert(d); // --> should be 1
  11. }
  12.  
  13. function2 (a, b)
  14. {
  15.     if (a < 5)
  16.     {
  17.        this.c = 0;
  18.     }
  19.     elseif (a > 10)
  20.     {
  21.        this.c = 1;
  22.     }
  23.     else
  24.     {
  25.        this.c = 2;
  26.     }
  27.  
  28.     if (b < 10)
  29.     {
  30.         this.d = 0;
  31.     }
  32.     elseif (b > 20)
  33.     {
  34.        this.d = 1 ;
  35.     }
  36.     else
  37.     {
  38.         this.d = 2;
  39.     }
  40. }
  41.  
  42.  
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
Jan 28 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
c and d are private variables. Unless the function is defined within the object function, it can't access them.
Jan 28 '08 #2

P: 18
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
Jan 30 '08 #3

gits
Expert Mod 5K+
P: 5,333
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
Jan 30 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.