- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2018 03:41 PM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2018 05:38 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2018 05:38 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2018 06:44 AM
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.