Getting function in script include method dynamically

Draes
Tera Expert

Hello,

I am trying to dynamically access a class-based script includes methods function(that's a mouthful), with no success. 

I'll explain it better in the code below:

var SoloClass = Class.create();
SoloClass.prototype = {
  initialize: function(method, innerFunc){
    return this[method](innerFunc);
  }, 
  myMethod: function(innerFunc){
    this[innerFunc]();
    function innerFunction() {
      var doSomeStuff = stuffHappensHere;
    }
  }, 
  type: 'SoloClass'
};
new SoloClass('myMethod', 'innerFunction');

Could someone point me in the right direction? I am able to reach the method and if i hardcode the function call it works, and the same approach works on a function outside the method/class so I assume it's a scoping issue I'm missing.

Thanks in advance for any help!

1 ACCEPTED SOLUTION

I'm not totally sure what you are trying to do, but I hope these resources help:

You can use the following code to extend (aka inherit) all functions of an existing class:

var MyClass = Class.create();
MyClass.prototype = Object.extendsObject(AnotherClass, {

Then, to call a function from AnotherClass within MyClass

function existingFuncInAnotherClass(parm1) {
   AnotherClass.prototpye.existingFuncInAnotherClass.call(this, parm1);
   //Additional code
}

So you could do this

var functionName = 'myFunction';
AnotherClass.prototpye.[functionName].call(this, parm1);

Or maybe look at the GlideEvaluator class


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

6 REPLIES 6

The SN Nerd
Giga Sage
Giga Sage

Are you trying to dynamically explore functions?
Have you tried using Xplore?

https://developer.servicenow.com/app.do#!/share/contents/9650888_xplore_developer_toolkit?v=4.05&t=PRODUCT_DETAILS


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

No I am trying to access functions with a variable name instead of hardcoding the function call.

I will however look into Xplore and see if that can help me find the issue, thanks for the tip.

Just curious .. what do you mean when you say "trying to access function with a variable name"

 

myMethod: function(innerFunc){
    this[innerFunc]();
    function innerFunction() {
      var doSomeStuff = stuffHappensHere;
    }

 

I'm trying to run the function innerFunction using a variable name instead of for example writing innerFunction();.

So to clarify the code this["Here is my variable"]() is supposed to call the function with the same name as the variable which is innerFunction.