Parameters 在Applescript中,为什么处理程序中的局部变量捕获;加上;标记参数?
在Applescript中,如果使用带“with”标签的参数声明处理程序,则局部变量将获取参数值,并且参数本身未定义。例如:Parameters 在Applescript中,为什么处理程序中的局部变量捕获;加上;标记参数?,parameters,applescript,local-variables,Parameters,Applescript,Local Variables,在Applescript中,如果使用带“with”标签的参数声明处理程序,则局部变量将获取参数值,并且参数本身未定义。例如: on bam of thing with frst and scnd local eat_frst return {thing: thing, frst:frst, scnd:scnd} -- this line throws an error end bam bam of "bug-AWWK!" with frst without scnd 导致错误
on bam of thing with frst and scnd
local eat_frst
return {thing: thing, frst:frst, scnd:scnd} -- this line throws an error
end bam
bam of "bug-AWWK!" with frst without scnd
导致错误消息,即在bam
的第二行中未定义“scnd”thing
和frst
都已定义,在调用bam
时传递参数。为什么会这样?为什么scnd
未定义
注意:我知道在处理程序中将变量声明为“局部”是不必要的。为了便于说明,在示例中进行了说明
这里还有一些不抛出错误的示例,说明了什么变量得到什么值。为了区分第一个和第二个给定参数,每个处理程序被调用,使用第一个给定参数,而不使用第二个给定参数。请注意,使用给定的userLabel:userParamName
语法在值捕获方面没有问题
on foo of thing given frst:frst_with, scnd:scnd_with
local eat_nothing
return {frst:frst_with, scnd:scnd_with}
end foo
on bar of thing with frst and scnd
local eat_frst
return {frst:eat_frst, scnd:scnd}
end bar
on baz of thing with frst and scnd
eat_frst
local eat_scnd, eat_others
return {frst:eat_frst, scnd:eat_scnd}
end baz
{foo:(foo of "foo" with frst without scnd), ¬
bar:(bar of "bar" with frst without scnd), ¬
baz:(baz of "baz" with frst without scnd)}
结果:
{foo:{frst:true,scnd:false},
条:{frst:true,scnd:false},
baz:{frst:true,scnd:false}
在使用它一段时间后,答案似乎是使用带有标签参数的不会引入变量。相反,这些值是按照它们在处理程序体中遇到的顺序分配给局部变量的
举例说明:
on baa of thing with frst and scnd
scnd
frst
return {frst:scnd, scnd:frst}
end baa
on bas of thing with frst and scnd
-- note that eat_frst gets the value of the frst parameter,
-- then gets set to "set"
set eat_frst to "set"
eat_scnd
return {frst:eat_frst, scnd:eat_scnd}
end bas
on qux of thing with frst and scnd
if scnd then
end if
local eat_scnd, eat_others
return {frst:scnd, scnd:eat_scnd}
end qux
on quux of thing with frst and scnd
if frst then
end if
if eat_scnd then
end if
return {frst:frst, scnd:eat_scnd}
end quux
{ baa: (baa of "baa" with frst without scnd), ¬
bas: (bas of "bas" with frst without scnd), ¬
qux: (qux of "qux" with frst without scnd), ¬
quux: (qux of "qux" with frst without scnd) }
结果:
{baa:{frst:true,scnd:false},
bas:{frst:“集”,scnd:false},
qux:{frst:true,scnd:false},
quux:{frst:true,scnd:false}
on baa of thing with frst and scnd
scnd
frst
return {frst:scnd, scnd:frst}
end baa
on bas of thing with frst and scnd
-- note that eat_frst gets the value of the frst parameter,
-- then gets set to "set"
set eat_frst to "set"
eat_scnd
return {frst:eat_frst, scnd:eat_scnd}
end bas
on qux of thing with frst and scnd
if scnd then
end if
local eat_scnd, eat_others
return {frst:scnd, scnd:eat_scnd}
end qux
on quux of thing with frst and scnd
if frst then
end if
if eat_scnd then
end if
return {frst:frst, scnd:eat_scnd}
end quux
{ baa: (baa of "baa" with frst without scnd), ¬
bas: (bas of "bas" with frst without scnd), ¬
qux: (qux of "qux" with frst without scnd), ¬
quux: (qux of "qux" with frst without scnd) }
{ baa:{frst:true, scnd:false},
bas:{frst:"set", scnd:false},
qux:{frst:true, scnd:false},
quux:{frst:true, scnd:false}}