问题描述
为什么不能声明和使用对变量的引用,除非所引用的变量是全局范围的?请解释导致以下现象的运行时内存或对象结构:
Why can't you declare and use references to variables unless the variable referenced is scoped globally? Please explain the runtime memory or object structure that leads to the following phenomenon:
脚本 A 失败:
on foo()
set l to {0}
set lref to a reference to l
return item 1 of lref
end foo
foo()
脚本 B 成功:
on run
set l to {0}
set lref to a reference to l
return item 1 of lref
end run
脚本 C 成功:
on foo()
global l
set l to {0}
set lref to a reference to l
return item 1 of lref
end foo
foo()
另见:您如何在 AppleScript 的处理程序中有效地构建列表? 和 为什么AppleScript不能在这个测试代码中将哈希的firstValue变成类型引用?
推荐答案
因为引用"与对象说明符是一回事,所以你不能引用不包含(或包含在就 AppleScript 而言,它不是)一个对象.
Because a "reference" is the same thing as an object specifier, so you can’t make a reference to something that isn’t (or is contained by something that isn’t) an object as far as AppleScript is concerned.
全局变量归顶级脚本对象所有——它实际上是一个没有初始化器的属性.(您也可以引用脚本属性;它不必是严格的全局属性.)
A global variable is owned by the top-level script object -- it’s really a property with no initializer. (You can also have a reference to a script property; it doesn’t have to be strictly global.)
另一方面,局部变量由它所在的处理程序的调用框架拥有,并且调用框架在 AppleScript 中不是对象,因此没有引用.
A local variable, on the other hand, is owned by the call frame of the handler that it’s in, and call frames are not objects in AppleScript, therefore, no references.
这篇关于为什么在 AppleScript 中不能声明对处理程序本地变量的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!