范围
范围是名称绑定有效的计算机程序区域。名称绑定只是说“变量”的一个花哨的词。
范围在编程应用程序中的范围可能有所不同。
这些可以从小的 for 循环和 if 语句到整个类。
范围级别
The rest of your class
<a name="kIhej"></a>
#### 类范围
类作用域是可以在类文件中的任何位置使用的变量或函数。
```swift
extends Node # Global Scope Class Name Node
# Class variables that can be used anywhere in the class file
var health: String = 100
var speed: float = 10.0
功能范围
只能在函数内部使用的变量/值
extends Node # Global Scope Class Name Node
#Class Scope
var health: String = 100
var speed: float = 10.0
func example():
# variable functionVariable can only be used inside of the function `example()`
var functionVariable = 10
代码块范围
只能在代码块内部使用的变量/值。
示例 fo 代码块将是 if 语句、match 语句、for & while 循环等:
extends Node # Global Scope Class Name Node
# Class Scope
var health: String = 100
var speed: float = 10.0
func example():
# Function Scope
var functionVariable = 10
if(true):
# Code Block Scope
# variable message exists only inside this one if statement
var message = "Hi!"
print(message)