All S# variables are non-typed. They can accept booleans, numbers, strings, and any other data type. Variables are stored in the script context, which means they have the same scope as the script in which they are declared.
Local Scope - Local Variables
When a script executes it runs in its own private variable scope.
Local variables declared within the private scope are available only until the script completes.
Best practice is to declare variables using the var keyword, but it is not strictly necessary.
For example, variables x and y are both declared within the context of the script snippet.
The test function has its own context and the variable z is available is declared there
and is limited to the scope of the function.
x = 5;
var y = 20;
var result = x * y;
DEBUG("The result is " + result);
function test() {
var z = 10;
}
DEBUG(z); /* generates error: 'Identifier not found: Namespace 'z' is not found' */
Since functions have their own context, they only have access to variables declared within.
For example, the test2 function does not have access to the variable msg.
var msg = "Hello World!";
function test2() {
DEBUG(msg); /* generates error: 'Identifier not found: Namespace 'msg' is not found' */
}
Local Scope - Intrinsic Objects
In addition to local variables, DataHub WebView also has a few special variables, known as intrinsic objects, which provide convenient access to common objects used by script bindings and event handlers. For example, this code could be used in a control event handler:
var thisPageName = Page.PageName; var thisElement = Element; var thisEvent = EventName;
thisPageName, thisElement, and thisEvent are local variables.
Page, Element and
EventName are intrinsic objects.
Intrinsic objects are only valid within the private scope. The availability of intrinsic objects depends on the
script context.
Global Scope
In addition to the private scope, all scripts have access to the global scope.
This global scope is shared across all scripts and all DataHub WebView pages for the duration of the WebView session.
The only way to reset the global scope is to re-start DataHub WebView.
Any variable used within a script that is not explicitly declared using the var keyword will refer to that variable in the global scope.
The simple act of assigning a value to a global variable will cause that variable to exist in the global scope.
Data Points
Data points are stored in a separate namespace and are not considered script variables. That is, data points can not be natively referenced in any scope. Instead, data points are referenced using the functions VAL, GET and SET. For more information, see Working with Data Points.
Dynamic Global Variables
Dynamic global variables are stored in a separate namespace, like data points. They are also referenced using the functions VAL, GET and SET.
Element Properties
Properties of controls are also not considered script variables. Like data points they can be referenced using special accessor functions, GETP and SETP. For more information, see Working with Element Properties.
