1

I was writing a Dialog component and this thought came into my mind suddenly.

export const alert = (content: string) => {
  const buttons = [<button onClick={()=>closeModal()}>ok</button>] // quite ok 
  // const buttons = [<button onClick={closeModal}>ok</button>] // raise an error
  const closeModal = modal(content, buttons)
}

The error is : Block-scoped variable 'closeModal' used before its declaration. I am so used to wrap some expression in a function in React and never thought about it.

The situation may be simplified to below:

const caller = () => {
  func() // ok
}
const funcAgain = func // error
func() // error
const func = () => {}

What's this behavior called? Does it have something to do with closure? or variable hoisting?

2 Answers 2

3

This has to do with the basics of The variable declaration and scope management and then execution phase. Here variable declarations for blocked scope variables(let , const), are actually hoisted but not initialised . Js engine simply denies any operation on uninitialized variable identifiers.

Also there is famous term for this called as Temporal Dead Zone(TDZ). func is in its TDZ in this case.

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

So inside caller, caller find the func in the TDZ. And js engine doesn't care whether func is initialized or not as long as func is declared?
nope, that's not accurate. It's cares, any variable declaration needs to be initialised before it's usage. Be it function declaration, variable declartion or block scoped declaration. You should read more about hoisting, that will be give you better picture.
got it. caller find the hoisted func which is not initialized yet in the global scope. But function means may called later, so it's ok. I tried if I turned caller to an IIFE, it would raised an error because func is not initialized.Thanks a lot
0

Good question. onClick on the first sample works because it returns a new Function object. The body of the function does not get evaluated until it is being invoked.

()=>closeModal()

I think you are referring to 'Hoisting'. To hoist a function, you need to declare as below

const caller = () => {
  func() // ok
}
func() 
function func () {}

1 Comment

the way that you typically pass in parameters that need to be evaluated later in Javascript is to pass through a function instead of passing in that expression directly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.