I've been using JSLint to make me feel bad about my JavaScript. It is great, by the way. There is one check that I don't quite understand and I'd like your views, please.
我一直在使用JSLint让我对我的JavaScript感到不好。顺便说一下,它很棒。有一张我不太明白的支票,请你的意见。
From jslint.com:
来自jslint.com:
In languages with block scope, it is usually recommended that variables be declared at the site of first use. But because JavaScript does not have block scope, it is wiser to declare all of a function's variables at the top of the function. It is recommended that a single var statement be used per function.
在具有块范围的语言中,通常建议在首次使用的站点声明变量。但是因为JavaScript没有块范围,所以在函数顶部声明所有函数的变量是明智的。建议每个函数使用一个var语句。
What is the last sentance in bold really saying? I think I should be declaring multiple variables like this?
大胆的最后一点是什么?我想我应该声明这样的多个变量?
var foo = 1, bar = 2;
And, is the "wise" part just a programming style to discourage errors down the line or is there more to it than that?
并且,“智慧”部分只是一种编程风格,可以阻止错误,或者还有更多的错误吗?
Thanks for your help.
谢谢你的帮助。
4 个解决方案
#1
72
The problem is that, whether you realise it or not, javascript invisibly moves all the var declarations to the top of the function scope.
问题是,无论你是否意识到,javascript都会无形地将所有var声明移动到函数作用域的顶部。
so if you have a function like this
所以,如果你有这样的功能
var i = 5;
function testvar () {
alert(i);
var i=3;
}
testvar();
the alert window will contain undefined. because internally, it's been changed into this:
警报窗口将包含undefined。因为在内部,它已被改为:
var i = 5;
function testvar () {
var i;
alert(i);
i=3;
}
testvar();
this is called "hoisting". The reason crockford so strongly advocates var declarations go at the top, is that it makes the code visibly match what it's going to do, instead of allowing invisible and unexpected behavior to occur.
这被称为“吊装”。 crockford如此强烈主张var声明的原因在于它使得代码明显与它将要做的事情相匹配,而不是允许发生隐形和意外行为。
#2
8
Basically in JavaScript blocks ({ ... }
) do not introduce a new scope, there is only function-scope, so no scope is created on any other statement.
基本上在JavaScript块({...})中不引入新的作用域,只有函数作用域,因此不会在任何其他语句上创建作用域。
A variable introduced anywhere in a function is visible everywhere in the function.
函数中任何位置引入的变量在函数中的任何位置都可见。
For example:
例如:
function myFunction(){
var one = 1;
if (one){
var two = one + 1;
}
(function () {
var three = one + two;
})();
// at this point both variables *one* and *two* are accessible but
// the variable *three* was declared in the scope of a inner function
// and is not accessible at this point.
}
In languages with block scope, it recommended to declare the variables at the point of first use, but since JavaScript does not have block scope, it is better to declare all of a function's variables at the top of the function.
在具有块作用域的语言中,建议在首次使用时声明变量,但由于JavaScript没有块作用域,因此最好在函数顶部声明所有函数的变量。
Check this article.
看看这篇文章。
#3
6
The lack of block scope explains the code below:
块范围的缺乏解释了以下代码:
var a = 1;
if (something) {
var a = 2;
}
alert(a); // Alerts "2"
In most C-style (as in syntax) languages, the var a = 2
definition would define 'a' only for the scope of the if
block. Using a single var statement at the top of the function helps to avoid this quirk of Javascript, which is not always as obvious as the above, and would be unexpected to C/C#/Java programmers.
在大多数C风格(如语法)语言中,var a = 2定义仅为if块的范围定义'a'。在函数顶部使用单个var语句有助于避免这种Javascript的怪癖,这并不总是如上所述,并且对于C / C#/ Java程序员来说是意外的。
#4
4
Yes, it means that you declare all variables at the beginning of the function. Whether you want to do it in one line or multiple lines is a matter of choice.
是的,这意味着您在函数的开头声明所有变量。无论您想要在一行还是多行中进行操作都是一个选择问题。
The reason is explained in the paragraph you mentioned. Javascript variables only have function level scope. If you declare the same variable inside an if/while/for block, it will be overwritten by the new value since the block doesn't carry a new scope. This is different from languages such as Java. To avoid such surprises, declare all the variables you are going to use in the function at the beginning of function so that you don't accidentally 'redeclare' andything.
原因在你提到的段落中解释。 Javascript变量只有函数级别范围。如果在if / while / for块中声明相同的变量,它将被新值覆盖,因为该块不带有新的作用域。这与Java等语言不同。为了避免这种意外,请在函数开头声明要在函数中使用的所有变量,这样就不会意外地“重新声明”和“重新声明”。
#1
72
The problem is that, whether you realise it or not, javascript invisibly moves all the var declarations to the top of the function scope.
问题是,无论你是否意识到,javascript都会无形地将所有var声明移动到函数作用域的顶部。
so if you have a function like this
所以,如果你有这样的功能
var i = 5;
function testvar () {
alert(i);
var i=3;
}
testvar();
the alert window will contain undefined. because internally, it's been changed into this:
警报窗口将包含undefined。因为在内部,它已被改为:
var i = 5;
function testvar () {
var i;
alert(i);
i=3;
}
testvar();
this is called "hoisting". The reason crockford so strongly advocates var declarations go at the top, is that it makes the code visibly match what it's going to do, instead of allowing invisible and unexpected behavior to occur.
这被称为“吊装”。 crockford如此强烈主张var声明的原因在于它使得代码明显与它将要做的事情相匹配,而不是允许发生隐形和意外行为。
#2
8
Basically in JavaScript blocks ({ ... }
) do not introduce a new scope, there is only function-scope, so no scope is created on any other statement.
基本上在JavaScript块({...})中不引入新的作用域,只有函数作用域,因此不会在任何其他语句上创建作用域。
A variable introduced anywhere in a function is visible everywhere in the function.
函数中任何位置引入的变量在函数中的任何位置都可见。
For example:
例如:
function myFunction(){
var one = 1;
if (one){
var two = one + 1;
}
(function () {
var three = one + two;
})();
// at this point both variables *one* and *two* are accessible but
// the variable *three* was declared in the scope of a inner function
// and is not accessible at this point.
}
In languages with block scope, it recommended to declare the variables at the point of first use, but since JavaScript does not have block scope, it is better to declare all of a function's variables at the top of the function.
在具有块作用域的语言中,建议在首次使用时声明变量,但由于JavaScript没有块作用域,因此最好在函数顶部声明所有函数的变量。
Check this article.
看看这篇文章。
#3
6
The lack of block scope explains the code below:
块范围的缺乏解释了以下代码:
var a = 1;
if (something) {
var a = 2;
}
alert(a); // Alerts "2"
In most C-style (as in syntax) languages, the var a = 2
definition would define 'a' only for the scope of the if
block. Using a single var statement at the top of the function helps to avoid this quirk of Javascript, which is not always as obvious as the above, and would be unexpected to C/C#/Java programmers.
在大多数C风格(如语法)语言中,var a = 2定义仅为if块的范围定义'a'。在函数顶部使用单个var语句有助于避免这种Javascript的怪癖,这并不总是如上所述,并且对于C / C#/ Java程序员来说是意外的。
#4
4
Yes, it means that you declare all variables at the beginning of the function. Whether you want to do it in one line or multiple lines is a matter of choice.
是的,这意味着您在函数的开头声明所有变量。无论您想要在一行还是多行中进行操作都是一个选择问题。
The reason is explained in the paragraph you mentioned. Javascript variables only have function level scope. If you declare the same variable inside an if/while/for block, it will be overwritten by the new value since the block doesn't carry a new scope. This is different from languages such as Java. To avoid such surprises, declare all the variables you are going to use in the function at the beginning of function so that you don't accidentally 'redeclare' andything.
原因在你提到的段落中解释。 Javascript变量只有函数级别范围。如果在if / while / for块中声明相同的变量,它将被新值覆盖,因为该块不带有新的作用域。这与Java等语言不同。为了避免这种意外,请在函数开头声明要在函数中使用的所有变量,这样就不会意外地“重新声明”和“重新声明”。