Strongly typed fields
At its core, JavaScript is a loosely typed language, which means you do not have to declare the data types of variables explicitly. JavaScript variables assume a data type based on the content that is placed in them. Â In many cases, JavaScript performs conversions automatically when they are needed. For example, if you add a number to an item that consists of text (a string), the number is converted to text automatically.
However, for business applications, being able to explicitly specify a data type is extremely useful. For example, you may want to strongly type an amount field, so that it can only hold numeric data with a specific precision (length and decimal positions). In a business application, putting character data into an amount field should raise an exception. It is for this reason that Profound.js introduces strong types to JavaScript. You can now use both loosely typed variables and strongly typed fields within your applications. Profound.js supports a variety of data types, including pointers, fixed length character fields, and data structures. In addition to making your application code more robust, this capability also allows for simple communication to take place between your Node.js applications and various legacy system resources, such as system API and programs on IBM i.
A strongly typed field is declared using the pjs.define() API.
The following examples illustrate the difference between loosely and strongly typed fields.
Loosely typed variable
var amount; // amount is a loosely typed variable and has not been initialized
amount = 125.99; // amount is now a number
amount = "ABC"; // this is valid because amount is loosely typed; it is converted to a string on the fly
Strongly typed field
pjs.define("amount", { type: "packed decimal", length: 7, decimals: 2 }); // amount is strongly typed and initialized to 0
amount = 125.99; // amount has changed from 0 to 125.99
amount = "ABC"; // this line of code will produce an error because amount can only hold numbers with a predefined precision
Running Profound.js module in "strict" mode.
Prior to version 5.0.0, Profound.js module can not be run in "strict" mode (with the statement "use strict" specified) since Profound.js uses the "with" keyword to scope variables defined with pjs.define() so that they can be referred to as "implicit" variables, but Javascript does not support the "with" keyword in "strict" mode. In version 5.0.0, Profound.js modules can be run in "strict" mode. For modules running in "strict" mode, the "with" keyword is not used to scope the variables defined with pjs.define(), which means you must use a different way to set/get those variables. To set the variable, use pjs.set() API. To get the variable, use pjs.get() API.
Example of a module not running in "strict" mode. You can set/get the variables using the implicit unqualified variable names.
function app() {
pjs.define("flda", {type: 'char', length: 3});
flda = 'ABC'; // setting an implicit variable
console.log(`flda = ${flda}`); // getting an implicit variable
}
exports.run = app;
Example of a module running in "strict" mode. You must set/get the variables using pjs.set() and pjs.get() APIs and the quoted variable names.
Note that prior to version 5.0.0, if a Profound.js module had the statement "use strict" specified, that statement was ignored so that the module could run in non-strict mode. In version 5.0.0, if a Profound.js module has the statement "use strict" specified, that statement is kept to enable the module to run in strict mode.