【发布时间】:2010-09-30 22:12:09
【问题描述】:
我对 VBScript 中的变量范围有疑问。我知道有以下关键字(来自autoitscript.com):
- Dim = 局部范围,如果变量名在全局尚不存在(在这种情况下,它会重用全局变量!)
- Global = 强制在全局范围内创建变量
- Local = 强制在 Local/Function 范围内创建变量
假设我有以下 .vbs 文件:
Dim strPath
strPath = "C:\folder"
DisplayPath strPath
Sub DisplayPath(strPath) 'Does this strPath get it's own local scope?
MsgBox strPath
End Sub
在函数中:DisplayPath(strPath),strPath 是 local 变量吗?或者函数/子程序是否可以访问在脚本主要部分顶部定义为 global 变量的strPath?
另外,显式使用Dim 而不是在我使用它们时定义变量(这在脚本语言中是可能的)有什么意义?
【问题讨论】: