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

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

Might have to do it that way that i store the other functions in another class and each "upper" function inherits relevant class.

I mean I'm basically trying to nest functions and then dynamically call the nested ones and the upper functions.

I'll give the class approach a shot instead. Thanks.