JavaScript 变量作用域问题:to var,还是不to var
非常感谢。我正在教科书上工作,他们使用一个函数调用另一个打开窗口的函数:
function rtest(){
content='dans window';
oneWindow=open("","Window 1","width=450,height=290");
newWindow(oneWindow);
}
function newWindow(x){
x.document.close();
x.document.open();
x.document.write(content);
x.document.close();
x.moveTo(20,20);
x.focus();
}
所以一切正常,但我的问题是: newWindow() 函数如何能够访问“contents”的内容“ rtest() 函数中的变量?为什么,如果我在“content”变量前面加上“var”,就像这样:
function rtest(){
**var content='dans window';**
oneWindow=open("","OneWindow","width=450,height=290");
newWindow(oneWindow);
}
...会抛出错误(并且新窗口的内容留空)吗?
谁能向我解释一下使用 var 和不使用 var 有什么区别?
谢谢你!
丹尼尔
Many thanks in advance. I'm working out of a schoolbook and they're using one function to call another which opens a window:
function rtest(){
content='dans window';
oneWindow=open("","Window 1","width=450,height=290");
newWindow(oneWindow);
}
function newWindow(x){
x.document.close();
x.document.open();
x.document.write(content);
x.document.close();
x.moveTo(20,20);
x.focus();
}
So everything works fine, but my question is this: how is the newWindow() function able to access the contents of the "contents" variable in the rtest() function? And why, if I preface the "content" variable with "var", like this:
function rtest(){
**var content='dans window';**
oneWindow=open("","OneWindow","width=450,height=290");
newWindow(oneWindow);
}
...does an error get thrown (and the contents of the new window left blank)?
Can anybody explain to me what the difference between using var and not using var is?
Thank you!
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您不在 rtest 中使用 var,它会自动成为全局变量。这就是为什么它可以从其他 javascript 代码(包括 newWindow)访问。现在,当您使用 var 时,它会自动成为 rtest 范围内的变量,因此现在可以使用它的是同一范围内的变量。
if you dont use var inside the rtest, it is automatically a global variable. which is why it is accessible from other javascript codes including newWindow. now, when you use var, it automatically a variable inside the rtest scope, so the ones that can use it now are those inside the same scope.
如果在原始函数内使用
var
声明变量,它将成为局部变量,并且在函数外部不可见。如果您根本不声明该变量,它将是全局的。然而,最佳实践是声明全局变量。如果您的教科书没有这样做,请考虑更换它。如果你的教授不这样做,请考虑更换(或改革)他。 :-) 如果您难以说服他,您可以(但不一定应该)提及我是这里的前 200 位用户之一。
例如:
另外,打开空白窗口的最佳方法是调用
open("about:blank", ...)
,而不是open("", ...)< /代码>。
If you declare the variable using
var
inside the original function, it will become a local variable and will not be visible outside the function.If you don't declare the variable at all, it will be global. However, best practice is to declare global variables. If your textbook doesn't do this, consider replacing it. If your professor doesn't do this, consider replacing (or reforming) him. :-) If you have trouble convincing him, you can (but not necessarily should) mention that I'm one of the top 200 users here.
For example:
Also, the best way to open a blank window is to call
open("about:blank", ...)
, notopen("", ...)
.这是关于函数范围的,如果您使用
var
声明变量,则它将仅在您执行此操作的函数范围内可用。如果不使用
var
语句,并且对未声明的标识符进行赋值(未声明的赋值),则该变量将作为 Global 对象的属性添加。It's about the function-scope, if you declare your variable with
var
, it will be available only in the scope of the function where you did it.If you don't use the
var
statement, and you make an assignment to an undeclared identifier (an undeclared assignment), the variable will be added as a property of the Global object.如果您不使用
var
,那么您将创建一个全局变量;也就是说,可以从程序中任何位置的任何代码访问的变量。如果您使用var
,您将创建一个 局部变量,该变量是一个只能在定义它的范围内访问的变量(通常是定义它的函数)。虽然全局变量一开始很方便,但使用它们通常不是一个好主意。问题是你的所有代码都将共享一个全局变量;将来,如果您出于某种原因需要该变量的两个或多个不同版本,您将无法区分这两种用途。全局变量也可以从程序中的任何位置访问或更改,因此很难弄清楚可能修改或依赖于哪些变量,而局部变量只能在代码中有限的、定义良好的部分内访问,这可以很容易被检查。
If you don't use
var
, then you are creating a global variable; that is, a variable that is accessible from any code anywhere in your program. If you usevar
, you are creating a local variable, which is a variable that is only accessible from within the scope in which it is defined (generally, the function it is defined in).While global variables can be convenient at first, it's generally a bad idea to use them. The problem is that all of your code will share that one global variable; in the future, if you need to have two or more different versions of that variable for whatever reason, you won't be able to separate the two uses. Global variables can also be accessed or changed from anywhere within your program, so it can be hard to figure out what might be modifying or depending on one, while local variables can only be accessed within a limited, well defined section in code, which can easily be inspected.
使用 var 您可以在函数中声明一个局部变量,因此该变量在该函数外部不可见。如果没有 var,您实际上是在处理窗口对象并设置或覆盖它的字段。客户端 Javascript 中的全局范围始终是 window 对象。所以你也可以写 window.content='dans window';以便更清楚地了解您实际上在那里做什么,但除此之外,它们将是相同的。顺便说一句:窗口变量本身只是窗口对象的一个字段,它递归地引用回窗口。
With var you declare a local variable in the function which is thus not visible outside this function. Without var you are actually working on the window object and set or overwrite a field of it. Your global scope in client side Javascript is always the window object. So you could also have written window.content='dans window'; to make clearer what you are actually doing there, but otherwise it would be identical. By the way: the window variable is itself just a field of the window object that refers recursively back to the window.