Javascript - Do I need to use 'var' when reassigning a variable defined in the function's parameters?

前端 未结 4 1114
情书的邮戳
情书的邮戳 2021-01-18 07:20

I\'ve found many definitions of the \'var\' statement but most of them are incomplete (usually from introductory guides/tutorials). Should I use \'var\' if the variable &

4条回答
  •  萌比男神i
    2021-01-18 07:55

    "I've found many definitions of the 'var' statement but most of them are incomplete."

    Try this (a bit stuffy but hopefully complete :-) ): The var keyword declares a new variable, binds it to the current lexical scope, & hoists it to the top of said scope. In other words, the variable will not exist outside of the function that declares it & will be declared before any statements are executed inside the function. Function parameters are implicitly bound to that function's scope so do not require the var keyword.

提交回复
热议问题