简体   繁体   中英

Accessing inner function variable in outer function in javascript?

I am trying to access the value of inner function variable outside. But I cannot use it outside the inner function. My code is-

function t9(){
   var start=298;
   var diff;
   function count(){
      var end=400;
      var diff=end-start;
   } 
   return diff;
}

Dont re-declare the variable, so instead of : var diff=end-start; write- diff=end-start;

Thats it

function t9()
  var start=298;
  return count(start);
}

function count(start){
   var end=400;
   var diff=end-start;
   return diff;
  } 
function t9(){
   var start=298;
   var diff;
   var end;
   function count(){

      end=400;
      diff=end-start;
   } 
   count();
   alert(diff);
}
t9();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM