Why are arguments in JavaScript not preceded by the var keyword?

后端 未结 4 392
萌比男神i
萌比男神i 2021-01-12 02:05

This may be a silly question, but why are function arguments in JavaScript not preceded by the var keyword?

Why:

function fooAnything(anything) {
  r         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-12 02:35

    I think the question comes up because we're used to seeing function bla (int i) in many languages. The same, syntactically, as the int i; somewhere in the function body to declare a variable. The two ints are however not doing the same; the first defines the type, the second defines type and the scope. If you don't have to declare the type, the scope-declaration still needs to happen (which is why we have var in the second case) but there is no information needed in front of arguments.

提交回复
热议问题