通过例子说明变量的作用域
1、
函数首行local xx, 除了xx为局部变量,其他全局变量和超级全局变量函数内可见
默认:全局变量函数内不可见,超级全局变量函数内可见
#include <log4ahk>
a := 1
b := 2
global c := 3
d := 4
my_func()
func2()
my_func()
{
local e
log.info("a = " a , ";b = " b, ";c = " c, ";d = " d)
}
func2()
{
log.info("a = " a , ";b = " b, ";c = " c, ";d = " d)
}
^r::
Reload
return
2、
函数首行 local,强制局部模式,全局变量和超级全局变量不可见,需要用global单独声明
#include <log4ahk>
a := 1
b := 2
global c := 3
d := 4
my_func()
func2()
my_func()
{
local
global log
log.info("a = " a , ";b = " b, ";c = " c, ";d = " d)
}
func2()
{
log.info("a = " a , ";b = " b, ";c = " c, ";d = " d)
}
^r::
Reload
return
3、
函数首行global, 外部所有全局变量和超级全局变量可见
#include <log4ahk>
a := 1
b := 2
global c := 3
d := 4
my_func()
func2()
my_func()
{
global
log.info("a = " a , ";b = " b, ";c = " c, ";d = " d)
}
func2()
{
log.info("a = " a , ";b = " b, ";c = " c, ";d = " d)
}
^r::
Reload
return
4、
global全局模式下,通过local 声明局部变量
#include <log4ahk>
a := 1
b := 2
global c := 3
d := 4
my_func()
func2()
my_func()
{
global
local a,c
log.info("a = " a , ";b = " b, ";c = " c, ";d = " d)
}
func2()
{
log.info("a = " a , ";b = " b, ";c = " c, ";d = " d)
}
^r::
Reload
return
建议函数首行用 local
学习