mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2025-03-23 18:52:31 +01:00
Merge pull request #195 from jjspace/patch-1
Custom function scope Doc adjustments
This commit is contained in:
@ -908,30 +908,30 @@ The example above prints the following in its log::
|
|||||||
15
|
15
|
||||||
11
|
11
|
||||||
|
|
||||||
|
For those with experience in other languages, especially Javascript it may be important to note that function declarations are not hoisted and must be declared BEFORE you use them.
|
||||||
|
|
||||||
|
print(hello());
|
||||||
|
|
||||||
|
function hello() {
|
||||||
|
return "world";
|
||||||
|
}
|
||||||
|
|
||||||
|
Will cause an error saying `variable hello not defined` whereas::
|
||||||
|
|
||||||
|
function hello() {
|
||||||
|
return "world";
|
||||||
|
}
|
||||||
|
|
||||||
|
print(hello());
|
||||||
|
|
||||||
|
Will print out
|
||||||
|
|
||||||
|
world
|
||||||
|
|
||||||
**Note about variable scope in functions:**
|
**Note about variable scope in functions:**
|
||||||
|
|
||||||
Functions can access "global" variables declared outside of the function's scope. However, they cannot change the value of any "global" variables.
|
Functions can access "global" variables declared outside of the function's scope. However, they cannot change the value of any "global" variables.
|
||||||
Any changes to "global" variables will only be applied locally to the function. This also means that any variable that is first defined inside a
|
Any changes to "global" variables will only be applied locally to the function.
|
||||||
function will NOT be accessible outside of the function.
|
|
||||||
|
|
||||||
For example, the following code::
|
|
||||||
|
|
||||||
function sum(values) {
|
|
||||||
res = 0;
|
|
||||||
for (i = 0; i < values.length; ++i) {
|
|
||||||
res += values[i];
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
print(res);
|
|
||||||
|
|
||||||
results in the following runtime error::
|
|
||||||
|
|
||||||
Script runtime error:
|
|
||||||
Server Ip: 75.7.4.1
|
|
||||||
Script name: test.script
|
|
||||||
Args:[]
|
|
||||||
variable res not defined
|
|
||||||
|
|
||||||
The following example shows that any change to "global" variable inside a function only applies in the function's local scope::
|
The following example shows that any change to "global" variable inside a function only applies in the function's local scope::
|
||||||
|
|
||||||
@ -950,6 +950,27 @@ Results in the following log::
|
|||||||
0
|
0
|
||||||
0
|
0
|
||||||
|
|
||||||
|
Furthermore, this also means that any variable that is first defined inside a function will NOT be accessible outside of the function as shown in the following example::
|
||||||
|
|
||||||
|
function sum(values) {
|
||||||
|
res = 0;
|
||||||
|
for (i = 0; i < values.length; ++i) {
|
||||||
|
res += values[i];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
print(res);
|
||||||
|
|
||||||
|
results in the following runtime error::
|
||||||
|
|
||||||
|
Script runtime error:
|
||||||
|
Server Ip: 75.7.4.1
|
||||||
|
Script name: test.script
|
||||||
|
Args:[]
|
||||||
|
variable res not defined
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
**Other Notes about creating your own functions:**
|
**Other Notes about creating your own functions:**
|
||||||
|
|
||||||
Defining a function does not create a Javascript function object in the underlying game code. This means that you cannot use any function
|
Defining a function does not create a Javascript function object in the underlying game code. This means that you cannot use any function
|
||||||
|
Reference in New Issue
Block a user