Profound.js Function Handling
Overview
In the Profound.js framework, understanding the distinction between function expressions and function declarations is crucial for effective programming. Here is a breakdown of how the Profound.js framework handles these two types of functions.
Function Declarations
Function declarations in the Profound.js framework are defined using the function keyword followed by the function name and parameters. These functions operate similarly to IBM i procedures, as they are assigned a distinct “scope”. In this scenario, “scope” ensures that IBM i-related fields are created and accessible during the function’s execution. While scope enables the Profound.js APIs to work as intended, it may also result in a performance slowdown when these functions are required to run iteratively or recursively.
function get_product(product_id) {
return pjs.query(`Select * from PRODUCTSP WHERE PRID = ${product_id}`);
}Function Expressions
Function expressions, on the other hand, involve assigning a function to a variable. In the Profound.js framework, function expressions lack “scope”, positioning them as a more lightweight alternative to function declarations. However, this lack of “scope” can also cause issues where function expressions that are not nested within function declarations do not have the necessary IBM i-related fields defined.
// These functions only work if they are nested within a function declaration OR if the database being queried is not on IBMi
const find_product = function(product_id) {
return pjs.query(`Select * from PRODUCTSP WHERE PRID = ${product_id}`);
};
const find_product_arrow = (product_id) => {
return pjs.query(`Select * from PRODUCTSP WHERE PRID = ${product_id}`);
};The function expressions above only work if they are nested within a function declaration, or if the database being queried is not on IBM i.
Correct Usage
To use the lightweight form safely on IBM i, nest the expressions inside a function declaration. They then share the declaration’s scope, and the IBM i-related fields are available to them:
// Nesting the function expression inside a function declaration gives it the
// declaration's scope, so the IBM i-related fields exist when it runs.
function get_product(product_id) {
const find_product = function(id) {
return pjs.query(`Select * from PRODUCTSP WHERE PRID = ${id}`);
};
return find_product(product_id);
}