pjs.refParm()
This method is used for passing a parameter by reference either to JavaScript functions or with pjs.call().
Parameter
"value" - The value to pass. Can be either a primitive value or expression, or a field name. Field names can be specified directly, or by using a string containing the field name. The field must already be defined using pjs.define() .
Only one field can be passed to pjs.refParm() at a time.
The ability to specify a primitive value or expression was added in 7.16.0 and requires use of a parameter definition object (see below). If the parameter definition object is not specified, then the value will be interpreted as a field name.
The ability to specify a field name directly was added in version 7.16.0. In prior versions you must pass a string containing the field name.
“Parameter definition”. An object containing a parameter definition. This allows you to specify a parameter type or length that is different than the field you are passing. This also allows you to pass primitive values and expressions by reference. The object can contain the following properties:
- const: Set to Boolean true to pass by read-only reference. Use this option when you want to pass a parameter of a different type/length than the program accepts or when you want to pass a primitive value or expression. Note: When passing a field name and the parameter definition type/length matches exactly to the field type/length, the reference will NOT be read-only and the callee can still modify the value.
- type/length/decimals/varying (see pjs.define() for details) can be used to specify the parameter data type and length. The type/length must match exactly to what the called program expects. If the value passed is of a different type/length, it will be converted to the type specified in the parameter definition object in a temporary/read-only buffer that will be passed by reference.
- like (see pjs.define() for details) is used to define the parameter like another field in the program. The referenced field format must match exactly to what the called program expects.
- likeDS (see pjs.define() for details) is used to define the parameter like a data structure in the program. The referenced DS format must match exactly to what the called program expects.
- likeRec (see pjs.define() for details) is used to define the parameter like a record format in the program. The referenced format must match exactly to what the called program expects.
The ability to use a parameter definition object was added in 7.16.0. In prior versions, you can only pass field names by reference, and the field type/length must match exactly to what the called program expects.
When using pjs.call(), the use of pjs.refParm() is optional. When a declared field is passed as a parameter to pjs.call(), it is automatically converted to use pjs.refParm() internally. The explicit use of pjs.refParm() may be more efficient in some cases.
Return Value
An object that is used to pass the value by reference.
RPG Equivalent:
PARM
Examples
Call an IBM i Program with By-Reference Parameter
// Define fields using pjs.define().
pjs.define("myField1", {type: "packed decimal", length: 7, decimals: 2});
pjs.define("myField2", {type: "packed decimal", length: 8, decimals: 3});
pjs.define("myField3", {type: "packed decimal", length: 12, decimals: 5});
// Call using default parameter wrap, pjs.refParm().
pjs.call('mypgm1', myField1);
// Call using explicit parameter wrap, pjs.refParm().
pjs.call('mypgm1', pjs.refParm("myField1"));
Call an IBM i Program with By-Reference Parameters
// Define fields using pjs.define().
pjs.define("myField1", {type: "packed decimal", length: 7, decimals: 2});
pjs.define("myField2", {type: "packed decimal", length: 8, decimals: 3});
pjs.define("myField3", {type: "packed decimal", length: 12, decimals: 5});
// Call using default parameter wrap, pjs.refParm().
pjs.call('mypgm2', myField1, myField2, myField3);
// Call using explicit parameter wrap, pjs.refParm().
pjs.call('mypgm2', pjs.refParm("myField1"), pjs.refParm("myField2"), pjs.refParm("myField3"));
Call a JavaScript Function with a By-Reference Parameter
// Define function double() that will receive a by reference parameter to multiply by two.
function double(number) {
// Define temporary field number as type float.
pjs.define("number", { type: 'float', length: 8, refParm: number });
// Double value of number.
number = number * 2;
// Because the field is by-reference, there is no need for a return statement.
}
// Define myNum field as type float.
pjs.define("myNum", { type: 'float', length: 8, initValue: 10 });
// The initial value of 10 of myNum is changed to 20 after the function executes.
double(pjs.refParm("myNum"));
Call IBM i Program with a By-Reference Parameter of Different Type Than Program Field
Call IBM i Program and Pass Primitive Value by Read-only Reference
Call IBM i Program Using “like” Parameter Definition