简体   繁体   English

为什么JavaScript中的参数前面没有var关键字?

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

This may be a silly question, but why are function arguments in JavaScript not preceded by the var keyword? 这可能是一个愚蠢的问题,但为什么JavaScript中的函数参数前面没有var关键字?

Why: 为什么:

function fooAnything(anything) {
  return 'foo' + anyThing;
}

And not: 并不是:

function fooAnything(var anything) {
  return 'foo' + anyThing;
}

I have a feeling the answer is because that's what the Spec says but still... 我有一种感觉,答案是because that's what the Spec says但仍然......

Most dynamicaly-typed programming languages don't have explicit vars in the argument list. 大多数动态类型编程语言在参数列表中没有显式变量。 The purpose of the var keyword is to differentiate between "I am setting an existing variable" and "I am creating a new variable" as in var关键字的目的是区分“我正在设置现有变量”和“我正在创建一个新变量”,如

var x = 17; //new variable
x = 18; //old variable

(Only few languages, like Python, go away with the var completely but there are some issues with, for example, closures and accidental typos so var is still widespread) (只有少数几种语言,比如Python,完全消除了var,但是存在一些问题,例如关闭和意外的拼写错误,因此var仍然很普遍)

But in an argument list you cannot assign to existing variables so the previous ambiguity does not need to be resolved . 但是在参数列表中, 您无法分配给现有变量,因此不需要解析先前的歧义。 Therefore, a var declaration in the arguments list would be nothing more then redundant boilerplate. 因此,参数列表中的var声明只不过是冗余样板。 (and redundant boilerplate is bad - see COBOL in exibit A) (冗余样板很糟糕 - 参见exibit A中的COBOL)


You are probably getting your idea from C or Java but in their case type declarations double up as variable declarations and the cruft in the argument lists is for the types and not for the declarations. 您可能从C或Java中获取了您的想法,但在它们的情况下,类型声明会将变量声明加倍,参数列表中的残差是针对类型而不是声明。

We can even get away with a "typeless, varless" argument list in some typed languages too. 我们甚至可以使用某些类型语言中的“无类型,无变量”参数列表。 In Haskell, for example, types can be inferred so you don't need to write them down on the argument list. 例如,在Haskell中,可以推断出类型,因此您不需要在参数列表中将它们写下来。 Because of this the argument list consists of just the variable names and as in Javascript's case we don't need to put the extraneous "let" (Haskell's equivalent to "var") in there: 因此,参数列表只包含变量名称,并且在Javascript的情况下,我们不需要在其中放置无关的“let”(Haskell等效于“var”):

f a b =  --arguments are still variables,
         -- but we don't need "let" or type definitions
   let n = a + b in  --extra variables still need to be declared with "let"
   n + 17

It would be a redundant use of the var keyword. 这将是var关键字的冗余使用。 Items that appear in the parentheses that follow a function name declaration are explicitly parameters for the function. 出现在函数名称声明后面的括号中的项目是函数的显式参数。

The var keyword declares the scope of the variable. var关键字声明变量的范围。 A function argument also introduces the scope for that argument. 函数参数还引入了该参数的范围。 Hence there's no need for it, since it serves the same function. 因此没有必要,因为它具有相同的功能。

I think the question comes up because we're used to seeing function bla (int i) in many languages. 我认为这个问题出现了,因为我们习惯于在许多语言中看到function bla (int i) The same, syntactically, as the int i; 语法上与int i; somewhere in the function body to declare a variable. 在函数体中的某个地方声明一个变量。 The two int s are however not doing the same; 这两个int s的然而,不是做同样的; 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. 如果您不必声明类型,则仍需要发生scope-declaration(这就是我们在第二种情况下使用var原因)但是在参数前面不需要任何信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM