Calling Script Includes variables from other function

MGanon
Tera Guru

How can Script Includes variables be called by other functions?

I have a Script Includes with several functions. Many of the data sources share a similar data source. Many of the functions generate some of the same variables. To avoid duplicating the scripting code required to populate the common variables, I want to write the script once and call that result as needed.

How?

For example,

function1
var A = lines of script to generate the "A" variable
var B = lines of script to generate the "B" variable
var C = lines of script to generate the "C" variable
var X = lines of script to generate

function2
var A = lines of script to generate the "A" variable
var B = lines of script to generate the "B" variable
var C = lines of script to generate the "C" variable
var Y = many lines of script to generate ""

function3
var A = lines of script to generate the "A" variable
var B = lines of script to generate the "B" variable
var C = lines of script to generate the "C" variable
var Z = many lines of script to generate ""

Because variables A, B, and C each require many lines and generate the same output from the same input for each function, I would like to write the code once and call the common variables as needed.

1 ACCEPTED SOLUTION

Uncle Rob
Kilo Patron

In the initialize secition of the script include define your variables A,B,C, but have them generated right then with private functions in the include.  

 

this.A = this.generateA();
this.B = this.generateB();

generateaA = function () {
//do stuff to make A;
//return results;
};

generateaB = function () {
//do stuff to make B;
//return results;
};

otherFunction = function(){
//do stuff using this.A or this.B
}

View solution in original post

12 REPLIES 12

Uncle Rob
Kilo Patron

In the initialize secition of the script include define your variables A,B,C, but have them generated right then with private functions in the include.  

 

this.A = this.generateA();
this.B = this.generateB();

generateaA = function () {
//do stuff to make A;
//return results;
};

generateaB = function () {
//do stuff to make B;
//return results;
};

otherFunction = function(){
//do stuff using this.A or this.B
}

I initialized the this.variables at the initialize section and set them all NULL. Outside of the initialize section I have a function to populated the common variables. Every other function that needs those common variables then calls my first function before proceeding. It works.

Thanks all

MGanon
Tera Guru

How do I pass in the source data? Can I calculate the common variables in a single script? In this case, each record in the source data is a string that I parse before modifying. The data string can have different numbers of elements but will always end consistently. Before I can parse the data, I have to first remove some of it. The variables need depend on the other variables to calculate.

Assuming data like this:
qwerty^qwerty^dog^horse|junk,junk
qwerty^qwerty^qwerty^cat^chicken|morejunk

 

var test_script = Class.create();
test_script.prototype = {

 initialize: function() {
      var dn = source.filedX;
       dn = dn.split("|")[0]      // remove unnecessary data from the end of the source data
       var split = dn.split("^"); // splits most of the remaining data into different elements to generate multiple variables
       this.A = split[split.length-1]; //populates variable "A"
       this.B = split[split.length-2]; //populates variable "B"
       // the variables need the previous variable to calculate correctly
 
 }

};

Script includes can receive parameters.  Pass it in from wherever you're calling the SI.