简体   繁体   中英

function hoisting - why is my function not defined properly?

 function goo(){ console.log(foo) console.log(foo()) { function foo() {return 'im foo'} } } goo()

I'm trying to see where the foo function definition is hoisted to when it's inside a block.

I've learnt that in non-strict mode a function definition is hoisted to the top of function scope. So I expected the foo function to be defined right after goo() is executed cuz I expect it to be hoisted to the top of the goo function.

After the hoisted definition of foo, console.log(foo) and console.log(foo()) would run and I expected it to each give me a function definition and 'im foo'.

But what ended up happening is that the console says foo is initialized to undefined and foo is not a function. I thought initalization to undefined only happened when a var variable is hoisted. When function definition is hoisted, don't they define themselves? I'm not sure why foo is defined to undefined. Console didn't raise a reference error that says foo is not declared. foo is declared alright but console says it's not a function. Why is it initalized to undefined when I wrote it to be a function that returns 'im foo' in the definition?

 function goo(){ console.log(foo) console.log(foo()) function foo() {return 'im foo'} } goo()

I expected it to run like this code above. The only difference between the two is that foo function is defined inside a block. But why would there be a difference between the two if the foo function is hoisted to the top of goo function? Does this mean I was wrong about function definition being hoisted to top of function scope?

Both the functions foo() and goo() are hoisted during the interpretation phase, but the only catch is that foo is kept inside a block scope and hence access is not possible and you receive an error. Remember that, Block Level Scoping always affects identifier resolution(nothing to do with Hoisting ). In this example, the function declarations are defined and hoisted but a function declared within a block may or may not be available outside the block.

function goo(){
    console.log(foo)
    console.log(foo())
    {
        alert(1)
        function foo() {return 'im foo'}
        
    }
}
goo()

is equal to:

function goo(){
    console.log(foo)
    console.log(foo())
    {
        function foo() {return 'im foo'}
        alert(1)
        
    }
}
goo()

but not equal to:

function goo(){
    {
        function foo() {return 'im foo'}
        alert(1)
        
    }
    console.log(foo)
    console.log(foo())
}
goo()

You can initialize your function before call it to resolve "something is not a function" error

function goo() {  
          function foo() {
           return "im foo"
   }     
  console.log(foo);   
  console.log(foo()); 
  } 
goo();    

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