Exception when calling script include from a different script include

patrickfitz
Kilo Contributor

Hi,

encountering a sporadic error when trying to call a different script include.

var mySI = "MyOtherSciptInclude";

var k = new global[mySI]();

In my testing, the above code will work 90% of the time. When it does fail, my try/catch reports: undefined is not a function

Any insight/suggestions would be appreciated.

Patrick

5 REPLIES 5

Tony Chatfield1
Kilo Patron

Hi, normally I would invoke a script include/function call with something like

var mySI = new MyOtherSciptInclude().myFunction();

// or

var mySI = new MyOtherSciptInclude();
var k =mySI.myFunction();

//Alternate for global scope

var mySI = new global.MyOtherSciptInclude().myFunction();

// or

var mySI = new global.MyOtherSciptInclude();
var k =mySI.myFunction();

 

MrMuhammad
Giga Sage

Are you reffering to different scope when you say different script include? 

If yes, then the syntax would be something like this. 

var SI = new global.script_include();

SI.function_name();

For different scopes we use API name of script include instead of Script include name. 

find_real_file.png

Thanks,

Sharjeel

Regards,
Muhammad

patrickfitz
Kilo Contributor

Thanks for the quick responses 🙂

Both script includes are in 'Global' and the reason for my code formatting is that I have my code in a for loop and the value of 'mySI' is different for each iteration. ie

 

for( mySI in this.myObj) {

// if i write mySI to logs here, it is correct, but can fail on next line

gs.log(mySI); // correct value
var k = new global[mySI]();

k.someFunction(parm1...);

}

 

Thanks

Patrick

  

 

 

MrMuhammad
Giga Sage

Hi Patrick,

What is the type of value in this.myObj i.e. function, string, object?

you can check the type of this.myObj by using typeof() function. like this

gs.info(typeof(this.myObj));

To call ScriptInclude with dynamic name on each iteration the name should be of type function. If it is already of type function update the line as below.

//var k = new global[mySI]();

var k = new this.myObj[mySI]();

If the type of this.myObj is a string then you will need to first convert it to type function. eval() is used to convert string to function but please mind eval() is never recommended and has high-security concerns. 

var str = eval(this.myObj[mySI]);

Let me know how it goes. If you still face any issue do let me know the type of value in this.myObj so that I can further help you on this.

 

Thanks & Regards,

Sharjeel 

 

Regards,
Muhammad