-2

Why does ss() have different answers, doesn't the function execute in where it was defined?

var scope="global";  

function t(){  
    console.log(scope);  

    function ss()
    {
        console.log(scope);  
    } 

    var scope="local";
    ss();
}

t(); 
ss();

It logs:

undefined
local
undefined
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Ding Wei
  • 3
  • 2
  • Do this: `var scope="global"; function t() { console.log(scope); function ss() { console.log(scope); } scope="local"; ss(); return null; } t();` since scope is defined as a global variable, you can't have duplicate initialization of the same variablr in a function. You can only change its value! – Anshul Sanghi Nov 07 '16 at 08:23

1 Answers1

-1

first you have a duplicate named variable, second the "local" is below the functions so it will read the ones above, if you want to pass them on you want an parameter in the functions like this:

var scope="global";  
function t(e) {  
  console.log(e);
}  
function ss(e) {
  console.log(e);  
} 

scope="local";
t(scope); 
ss(scope);
Kevin Kloet
  • 1,086
  • 1
  • 11
  • 21
  • You're completely missing the point of this question – Cerbrus Nov 07 '16 at 08:08
  • you mistook my question,but no one answers,i guess itβ€˜s a bug of chrome console,i run the same code in the different window of chrome,it shows different result.but now it works,maybe,i can't execute a function that belongs to another function; – Ding Wei Nov 07 '16 at 09:27