javascript inner function variable accessibility

oggiemc
oggiemc used Ask the Experts™
on
Suppose i have the following

function outerFunction(){

   function innerFunction(){
        var x;
       }

}

I there anyway of making x available to outerFunction, without passing it as an argument??


Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Put it's declaration outside the functions.  http://www.w3schools.com/js/js_variables.asp
var x = 0;
function outerFunction(){

   function innerFunction(){
        x = 1;
       }

}

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
return it...

function outerFunction(){

   function innerFunction(){
        var x;
x = 2;
return x; 
       }

a = innerFunction();
alert(a);

}

Open in new window


do a test using : outerFunction();
you shoud get an alert with 2
HainKurtSr. System Analyst

Commented:
or declare it inside outerFunction


function outerFunction(){
  var x = 0;
  function innerFunction(){
    x = 1;
  }
}

Open in new window

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
Thanks for answers guys..

Dave
Im aware of that, but its not really what i want..

Leakim
Yeah, i actually just thought of that after i posted the question :) Suppose there were lots of variables including arrays etc..I could return all the variables??

i.e return(string, number, array)??

Thanks
HainKurtSr. System Analyst

Commented:
to test:

(it should say 0,0,1,1)
<script>
function outerFunction(){
  var x = 0;
  function innerFunction(){
  	alert('inside before setting: ' + x);
    x = 1;
  	alert('inside after setting: ' + x);
  }
  alert('outside before calling innerFunction: ' + x);
  innerFunction();
  alert('outside after calling innerFunction: ' + x);
}
outerFunction()
</script>

Open in new window

Sandeep KothariProject Lead
Commented:
what you need as javascript closure. .. have a look at it ..

http://blog.morrisjohns.com/javascript_closures_for_dummies.html
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
'return' only returns a single value.  http://www.w3schools.com/js/js_functions.asp  Besides, I consider 'returning a value' as passing it back so I didn't think that was what you wanted.
Multitechnician
Top Expert 2014
Commented:
>Yeah, i actually just thought of that after i posted the question :) Suppose there were lots of variables including arrays etc..I could return all the variables??

you can return the object itselft (this if you use this.x = 2) or an object similar to a hash table/json object

return {"x":x, "y":y}

Author

Commented:
HainKurt
How does that work? I thought that outside function cant have access inner function??

kshna
Thanks..il have a look at that :)
HainKurtSr. System Analyst
Commented:
inner function is using the variable defined in outer function :) not the reverse...

Author

Commented:
HainKurt
ahh, touche!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial