To allow JScript.NET to execute with optimal performance, the JScript .NET in Forsta surveys will be compiled in fast mode. Since fast mode places some restrictions on the type of code allowed, programs can be more efficient and execute faster. However, some features are not available in fast mode.
In fast mode, the following JScript behaviors are triggered:
- All variables must be declared.
- Functions become constants.
- Intrinsic objects cannot have expanded properties.
- Intrinsic objects cannot have properties listed or changed.
- The arguments object is not available.
- Cannot assign to a read-only variable, field, or method.
- eval method cannot define identifiers in the enclosing scope.
- eval method executes scripts in a restricted security context.
Most of these changes should not affect scripts used in Forsta Plus.
Important!
The eval method will not generate errors if used in Forsta Plus, however it is not supported by Forsta Plus and for security reasons should not be used in Forsta Plus surveys.
More details on those requirements that are most relevant when working with scripts, follow.
All Variables Must Be Declared
Previous versions of JScript did not require explicit declaration of variables. Although this feature saves keystrokes for programmers, it also makes it difficult to trace errors. For example, you could assign a value to a misspelled variable name, which would neither generate an error nor return the desired result. Furthermore, undeclared variables have global scope, which can cause additional confusion. So where previously you could write script code such as
codes = f("q1").categories();for(i=0;i<codes.length;i++) { <some statements…> }you must now declare the variables with the var keyword:
var codes = f("q1").categories();for(var i=0;i<codes.length;i++) { <some statements…> }This will still not explicitly declare the type of the variable: It may change type later as a result of an assignment. In addition to this "loose typing", JScript .NET can now be a strongly typed language. JScript .NET provides more flexibility than previous versions of JScript by allowing variables to be type annotated. This binds a variable to a particular data type, and the variable can store only data of that type. Although type annotation is not required, using it helps prevent errors associated with accidentally storing the wrong data in a variable and can increase program execution speed (go to About Types, Variables and Constants for more information).
In addition to this explicit type declaration, a technology called "implicit type inferencing" is introduced. Type inferencing analyzes your use of variables in the script code and infers the type of the variable for you. This means that you can achieve considerable improvements in speed using scripts also when you do not specify the type of your variables, as long as your variables are not changing type.
Functions Become Constants
In previous versions of JScript, functions declared with the function statement were treated the same as variables that held a Function object. In particular, any function identifier could be used as a variable to store any type of data.
In fast mode, functions become constants. Consequently, functions cannot have new values assigned to them or be redefined. This prevents the accidental changing of the meaning of a function (either a user-defined or a Forsta Plus function). You will now not be allowed to define several functions with the same name in the same project, or reuse the name of a function as a variable name. This should not cause particular problems for scripts used in Forsta Plus surveys, as it is just a bad programming habit to reuse function names anyhow. Disallowing this will avoid errors when it is not clear what function definition to refer to. It will also make it impossible to make one of your own or the standard functions in Forsta Plus inaccessible because it has accidentally been redefined due for example to it being used as a variable name.
The Arguments Object is not Available
Previous versions of JScript provided an arguments object inside function definitions, which allowed functions to accept an arbitrary number of arguments. The arguments object also provided a reference to the current function as well as the calling function.
In fast mode, the arguments object is not available. However, JScript .NET allows function declarations to specify a parameter array in the function parameter list. This allows the function to accept an arbitrary number of arguments, thus replacing part of the functionality of the arguments object (go to Functions with a Variable Number of Arguments for more information).
Note: It is not possible to directly access and reference the current function or calling function in fast mode.
Using a Sorting Function in the Sort Method on Array
Arrays can be sorted with the sort method. This method takes an optional function name as parameter, a function that defines how to sort elements if they are not to be sorted conventionally.
A script node is wrapped inside a class, so when you refer to the sort function you must use the keyword this to refer to the current instance.
array.sort ({this.sortFunction})