Noob Javascript Question - Passing an Object and Invoking a Method on that Object

G24
Kilo Sage

Hello.  It's been a very long day.  Can someone please help with this:

 

 

//This works.
var gdt = new GlideDateTime("2023-03-11 7:00:00");
gdt.addDaysUTC(1);
gs.addInfoMessage("First Value Displayed: " + gdt);

//This does NOT work.
myArbitraryFunction(gdt, "addDaysUTC", 1);

//This does NOT work.
myArbitraryFunction(gdt, addDaysUTC, 1);

//This does NOT work.
myArbitraryFunction(gdt, GlideDateTime.addDaysUTC, 1);

//This does NOT work.
myArbitraryFunction(gdt, GlideDateTime.prototype.addDaysUTC, 1);

function myArbitraryFunction(anyObject, anyFunction, anyParameter){
	gs.addInfoMessage("Got Here.");
	anyObject.anyFunction(anyParameter);
	gs.addInfoMessage("Second Value Displayed: " + gdt);
}

 

 

 

I'm running this in session, server side, using the "Debug Now" button from a Scheduled Script Execution.  Here is a Screenshot:

MyScreenshot.png

I looked on W3Schools and Stack Overflow, but I just cannot seem to understand the examples there.

How can I properly 1) modify my function and 2) properly CALL the function to get the date to increment.

Please help.  Thanks.

1 ACCEPTED SOLUTION

Peter Bodelier
Giga Sage

Hi @G24,

 

Try this:

//This works.
var gdt = new GlideDateTime("2023-03-11 7:00:00");
gdt.addDaysUTC(1);
gs.addInfoMessage("First Value Displayed: " + gdt);

myArbitraryFunction(gdt, "addDaysUTC", 1);

function myArbitraryFunction(anyObject, anyFunction, anyParameter){
	gs.addInfoMessage("Got Here.");
	anyObject[anyFunction](anyParameter);
	gs.addInfoMessage("Second Value Displayed: " + gdt);
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

2 REPLIES 2

Peter Bodelier
Giga Sage

Hi @G24,

 

Try this:

//This works.
var gdt = new GlideDateTime("2023-03-11 7:00:00");
gdt.addDaysUTC(1);
gs.addInfoMessage("First Value Displayed: " + gdt);

myArbitraryFunction(gdt, "addDaysUTC", 1);

function myArbitraryFunction(anyObject, anyFunction, anyParameter){
	gs.addInfoMessage("Got Here.");
	anyObject[anyFunction](anyParameter);
	gs.addInfoMessage("Second Value Displayed: " + gdt);
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

G24
Kilo Sage

@Peter Bodelier You are a gentleman and a scholar, Sir.  You have made my day, and my week.

May your wit charm the ladies, and may the cards fall your way.