IF 语句中的 javascript 变量范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6964895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript variable scope in the IF statement
提问by Joseph
Are variables declared and assigned in an "if" statement visible only within that "if" block or within the whole function?
在“if”语句中声明和分配的变量是否仅在该“if”块或整个函数内可见?
Am I doing this right in the following code? (seems to work, but declaring "var structure" multiple times seems awkward) any cleaner solutions?
我在下面的代码中这样做对吗?(似乎有效,但多次声明“var 结构”似乎很尴尬)任何更简洁的解决方案?
function actionPane(state) {
if(state === "ed") {
var structure = {
"element" : "div",
"attr" : {
"class" : "actionPane"
},
"contains" : [{
"element" : "a",
"attr" : {
"title" : "edit",
"href" : "#",
"class" : "edit"
},
"contains" : ""
}, {
"element" : "a",
"attr" : {
"title" : "delete",
"href" : "#",
"class" : "delete"
},
"contains" : ""
}]
}
} else {
var structure = {
"element" : "div",
"attr" : {
"class" : "actionPane"
},
"contains" : [{
"element" : "a",
"attr" : {
"title" : "save",
"href" : "#",
"class" : "save"
},
"contains" : ""
}, {
"element" : "a",
"attr" : {
"title" : "cancel",
"href" : "#",
"class" : "cancel"
},
"contains" : ""
}]
}
}
return structure;
}
采纳答案by OverZealous
1) Variables are visible for the whole function scope. Therefore, you should only declare them once.
1)变量对整个函数作用域都是可见的。因此,您应该只声明一次。
2) You should not declare the variable twice in your example. I'd recommend declaring the variable at the top of the function, then just setting the value later:
2)你不应该在你的例子中两次声明变量。我建议在函数顶部声明变量,然后稍后再设置值:
function actionPane(state) {
var structure;
if(state === "ed") {
structure = {
...
For excellent feedback on JavaScript, I highly recommend using JSLintby Douglas Crockford. It will scan your code for common errors, and find suggestions for cleanup.
对于 JavaScript 的优秀反馈,我强烈推荐使用Douglas Crockford 的JSLint。它将扫描您的代码以查找常见错误,并找到清理建议。
I also recommend reading the small book JavaScript: The Good Parts. It contains a lot of tips for writing maintainable JS code.
我还推荐阅读小书JavaScript: The Good Parts。它包含许多编写可维护 JS 代码的技巧。
回答by karim79
JavaScript has no "block scope", it only has function scope - so variables declared inside an if statement (or any conditional block) are "hoisted" to the outer scope.
JavaScript 没有“块作用域”,它只有函数作用域——所以在 if 语句(或任何条件块)中声明的变量被“提升”到外部作用域。
if(true) {
var foo = "bar";
}
alert(foo); // "bar"
This actually paints a clearer picture (and comes up in interviews, from experience :)
这实际上描绘了一幅更清晰的图景(并且在采访中出现,根据经验:)
var foo = "test";
if(true) {
alert(foo); // Interviewer: "What does this alert?" Answer: "test"
var foo = "bar";
}
alert(foo); // "bar" Interviewer: Why is that? Answer: Because JavaScript does not have block scope
Function scope, in JavaScript, typically refers to closures.
在 JavaScript 中,函数作用域通常是指闭包。
var bar = "heheheh";
var blah = (function() {
var foo = "hello";
alert(bar); // "heheheh"
alert(foo); // "hello" (obviously)
});
blah(); // "heheheh", "hello"
alert(foo); // undefined, no alert
The inner scope of the function has access to the environment in which it is contained, but not the other way around.
函数的内部作用域可以访问它所在的环境,但反过来不行。
To answer your second question, optimisation can be achieved by initially constructing a 'minimal' object which satisfies all conditions and then augmenting or modifying it based on particular condition(s) which has/have been satisfied.
为了回答您的第二个问题,可以通过最初构建满足所有条件的“最小”对象,然后根据已经/已经满足的特定条件对其进行扩充或修改来实现优化。
回答by TheDarkIn1978
回答by Eric Conner
Variables declared inside the if statement will be available outisde as long as they reside in the same function.
在 if 语句中声明的变量在外部可用,只要它们位于同一个函数中。
In your case, the best way would be to declare structure and then modify the parts of the object that differ in either case:
在您的情况下,最好的方法是声明结构,然后修改在任何一种情况下都不同的对象部分:
var structure = {
"element" : "div",
"attr" : {
"class" : "actionPane"
},
"contains" : [{
"element" : "a",
"attr" : {
"title" : "edit",
"href" : "#",
"class" : "edit"
},
"contains" : ""
}, {
"element" : "a",
"attr" : {
"title" : "delete",
"href" : "#",
"class" : "delete"
},
"contains" : ""
}]
}
if(state != "ed") {
// modify appropriate attrs of structure (e.g. set title and class to cancel)
}
回答by foxy
are variables declared and assigned in an "if" statement visible only within that "if" block or within the whole function?
在“if”语句中声明和分配的变量是否仅在该“if”块或整个函数内可见?
In Javascript, all variables are either
在 Javascript 中,所有变量都是
- global scope
- local scope (entirefunction) - Javascript doesn't have a "block scope" where variables are only available with a smaller block of the local scope (function)
- 全球范围
- 局部作用域(整个函数) - Javascript 没有“块作用域”,其中变量仅适用于局部作用域(函数)的较小块
am i doing this right in the following code? (seems to work, but declaring "var structure" multiple times seems akward) any cleaner solutions?
我在下面的代码中这样做对吗?(似乎有效,但多次声明“var 结构”似乎很尴尬)任何更清洁的解决方案?
Yes. A cleaner solution might be to build a base class of structure
, and modify what is different in each case.
是的。更简洁的解决方案可能是构建 的基类structure
,并修改每种情况下的不同之处。