Variable Declarations
- HFL supports both the javascript 'var' and 'let' declarations of variables, including their lexical scoping behavior. Thus variables declared with either declaration outside of functions or blocks enclosed in braces will be global in scope.
- Variables declared using 'var' within functions or scope specifications will be global to the function or scope (i.e. will have function scope), regardless of where they are declared within the function or scope.
- Variables declared using 'let' will only be visible to the immediately-enclosing block inside the innermost set of braces (i.e. will have block scope).
-
Variables in HFL have some slight behavioral differences from javascript:
- Variables whose names do not begin with a '$' must be declared. Thus '$foo' would not have to be declared, but 'bar' would.
- Variables whose names begin with a '$' followed by one or more alphanumeric characters can be used within double-quoted strings and will be replaced with their value.
- Variables that are not declared are true global variables, not properties of the global object.
-
EXAMPLES:
-
// Variables without leading '$' must be declared
var count = 1; // Declared global var
count++;
// Variables with leading '$' will expand in double-quoted strings
$wld = 'World'; // Undeclared global var
print("Hello $wld!"); // Prints "Hello World!"
// Variables can take arrays as values
var arr = [0, 1+3, 2]; // Array
var $type = arr[1]; // Access array element
print("Type is $type"); // Prints "Type is 4"
test_nested_vars(); // Call test function
// Variables within a function follow javascript 'var' and 'let' scope rules
function test_nested_vars() {
var $a = 'the', $b = ' quick', $c = ' blue', $d = ' dolphin', $e = ' jumped';
print("$a$b$c$d$e$f");
if ($b) {
let $b1 = ' slow'; // Has block scope
var $c = ' brown'; // Overrides $c
var $f = ' lazy dog'; // Has function scope
print("$a$b1$c$f");
}
print("$a$b$b1$c$d and the sea");
nested();
function nested () { // Inner-function closure
var $d = ' fox', $g = ' walked'; // Has nested function scope
print ("$a$b$c$d$g")
}
print("$a$b$c$d$e over the$f$g");
}
-
-
GENERATES OUTPUT:
-
Hello World!
Type is 4
the quick blue dolphin jumped
the slow brown lazy dog
the quick brown dolphin and the sea
the quick brown fox walked
the quick brown fox jumped over the lazy dog
-
Variable Utility Functions
- Below is a list of utility functions for variables built into HFL.
-
- Returns true if the variable name is currently defined, false if not.
- The <var-name> name string should be unquoted.
-
- Set the value of the variable given by <var-name> to be undefined.
- The <var-name> name string should be unquoted.