0

This is probably basic... given a fully qualified function name, I'm trying to call it via .apply()

Superficial example

var ns = {
    someFunc: function() { console.log('stuff'); }
}

function someTopLevelFunc() { console.log('top level'); }

Given a method that takes the name of that function, us there some way to call the namespaced function?

function theCaller(funcName) {
  console.log("Callback name: " + callbackFunctionName)
  const callbackFunction = window[callbackFunctionName];
  console.log("Type: " + (typeof callbackFunction))

  if((typeof callbackFunction) === "function") {
     callbackFunction.apply(null, []);
  }
}

theCaller('topLevelFunc');
theCaller('ns.someFunc');

The first call works while the second call shows the callbackFunction type as undefined

Probably a simple syntax issue, but my googling has failed me.

Trebla
  • 1,164
  • 1
  • 13
  • 28
  • Why do you need these dynamic function names to begin with? Use a single object and access the property name like this: `const ns = { someFunc(){} }; const funcName = "someFunc"; ns[funcName]();`. – Sebastian Simon Dec 07 '22 at 16:13
  • 1
    This may be a pedantic nitpick, but the function is a method of the `ns` object. I'm not sure "namespaced" is the best way to think about it – evolutionxbox Dec 07 '22 at 16:14
  • This is a superficial example. In the real case, the calling function is not in the same application. Trying to provide a generic ability to callback a named function in any namespace. Or are you suggesting to pass a reference to the function instead of the name? – Trebla Dec 07 '22 at 16:16
  • Perhaps calling is "namespaced" is wrong... I agree. I'm still interested in if possible (and how) to call a nested function in an object given its fully qualified reference name. Whatever the best way to describe it is – Trebla Dec 07 '22 at 16:20
  • 2
    Does this question provide you with an answer? [Convert a JavaScript string in dot notation into an object reference](https://stackoverflow.com/a/6394168/2181514) – freedomn-m Dec 07 '22 at 16:21
  • In JavaScript, if you want to call a function, you either have to directly obtain a reference to this function, or the name of the property holding the function paired with the object that has the property. You normally don’t use a string like `"ns.someFunc"`. What specifically do you mean by _“the calling function is not in the same application”_? If you can’t get an object reference, then at least have one fixed object that holds all functions as properties and refer to the function you need by key name. – Sebastian Simon Dec 07 '22 at 16:24
  • The question linked by freedomn-m is likely a sufficient answer. To Sebastion Simon, "theCaller" is completely unaware of the "ns" object in this case. It's a generic JavaScript library which provides some processing, and can callback a given function after processing completes. In this case we have a handful of grails applications and this javascript is provided by a plugin. The plugin needs to call a JavaScript function defined in the parent application given the name. – Trebla Dec 07 '22 at 16:32
  • @freedomn-m if you'd like to post that link as an answer I'll mark this resolved. – Trebla Dec 07 '22 at 17:24

0 Answers0