20,741
edits
m (→Namespaces) |
m (→Namespaces) |
||
| Line 71: | Line 71: | ||
Before namespaces were used, there were only such "global variables", so whenever some piece of code referred to a variable, it was possible to clash with some other similar or even unrelated uses of the variable (imagine a counter variable). | Before namespaces were used, there were only such "global variables", so whenever some piece of code referred to a variable, it was possible to clash with some other similar or even unrelated uses of the variable (imagine a counter variable) somewhere else in the source code. | ||
That's when people started providing a surrounding "context" to embed variables properly. | That's when people started providing a surrounding "context" to embed variables properly. | ||
| Line 77: | Line 77: | ||
<syntaxhighlight lang="php"> | |||
var hello = func { | |||
var x =100; # x is local variable specific to the scope of the currently executing function | |||
} | |||
</syntaxhighlight> | |||
Here, x is declared to be specific to the "hello" function and its namespace, 'x' will only be valid and visible during the lifetime of function, i.e. at execution time. Consequently, this means that accesses to such variables will be invalid: | |||
<syntaxhighlight lang="php"> | |||
var hello = func { | var hello = func { | ||
var x =100; | var x =100; # x is local variable specific to the scope of the currently executing function | ||
} | } | ||
hello(); | |||
print(x); | |||
</syntaxhighlight> | |||
In Nasal, a namespace is just a conventional hash: | In Nasal, a namespace is just a conventional hash, which is a fancy word for a dictionary. A dictionary stores values in the form of key/value pairs, each value is linked to a key that can be used to look up said value: | ||
<syntaxhighlight lang="php"> | |||
var foo = {}; | var foo = {}; | ||
</syntaxhighlight> | |||
To add members or fields to this "context" (or namespace), you can use several different notations, such as using conventional assignment: | |||
<syntaxhighlight lang="php"> | |||
foo.altitude = 100; | |||
foo.altitude = 100; | |||
foo["altitude"] = 100; | foo["altitude"] = 100; | ||
<syntaxhighlight lang="php"> | |||
or even specify fields during initialization: | or even specify fields during initialization using a key, colon, value notation (key:value): | ||
<syntaxhighlight lang="php"> | |||
var foo = { altitude:100 }; | var foo = { altitude:100 }; | ||
</syntaxhighlight> | |||
To add multiple keys to such a dictionary, you just separate them using a comma: | |||
<syntaxhighlight lang="php"> | |||
var foo = { altitude:100, latitude:0, longitude:0 }; | |||
</syntaxhighlight> | |||
To make this easier you can always add a trailing comma, which is still valid Nasal: | |||
<syntaxhighlight lang="php"> | |||
var foo = { altitude:100, latitude:0, longitude:0, }; | |||
</syntaxhighlight> | |||
In addition, it is valid to omit the key's value too: | |||
<syntaxhighlight lang="php"> | |||
var foo = { altitude:, latitude:, longitude:, }; | |||
</syntaxhighlight> | |||
Which will be equivalent to initializing the value to '''nil''': | |||
<syntaxhighlight lang="php"> | |||
var foo = { altitude:nil, latitude:nil, longitude:nil, }; | |||
</syntaxhighlight> | |||
In order to access these fields or "members" of a namespace, you need to provide | In order to access these fields or "members" of a namespace, you need to provide a valid namespace first: | ||
print ( foo.altitude ); | print ( foo.altitude ); | ||