在 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
导致错误消息“scnd”未在bam
. thing
并且frst
都被定义,获取在调用中传递的参数bam
。为什么会这样?为什么是scnd
未定义的?
注意:我知道在处理程序中将变量声明为“本地”是不必要的。出于说明目的,它在示例中完成。
这里还有一些不会引发错误的示例,说明什么变量得到什么值。为了区分第一个和第二个给定参数,每个处理程序都被调用with
第一个给定参数和without
第二个给定参数。请注意,使用该语法在值捕获方面没有问题。given 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}, bar:{frst:true, scnd:false}, baz:{frst:true, scnd:false}}