0

I have modularized my Javascript code in this style:

var groupHandler = function( constructorOptions )
{
    "use strict";

    var init = function( optionsToSet )
    {
        jQuery.extend( options, optionsToSet);
        return this;
    };


    var newGroup = function()
    {

    }

    var call = {
        init: init,
        options: options,
        newGroup: newGroup
    };

    if(typeof myPublicTestNamespace == "undefined"){//http://stackoverflow.com/a/9172377/123594
        return {
            init: init,
            newGroup: newGroup
        };
    }else{
        return call;
    };

    init( constructorOptions );
};

In one of my modules I have a list of functions from other modules to call like this:

validatorFunctions = call.getLocalStorageArray( 'validatorFunctions', model);
for (var f=0;f < validatorFunctions.length;f++){
    if (callFunction = call.getFunction( validatorFunctions[f] )){
        valid = callFunction( loopRowId, fields, call );
        if (!valid) break;
    }
}

I'd like to be able to call functions in other modules by using a "." syntax in my function call name:

var getFunction = function( functionName )
{
    if (functionName.indexOf( '.' ) != -1){
        var functionParts = functionName.split( '.' );
        var classFunction = functionParts[1];
        if (typeof window[functionParts[0]] === "function") {
            var obj = new window[functionParts[0]]();
            return obj['classFunction'];                       <!----- how to return function here?
        }
    }else{
        if (typeof (window[functionName]) === "function") {
            return window[functionName];
        }
    }
    return false;
};

but I can't figure out how to return the function based on the class object and the function name?

jdog
  • 2,465
  • 6
  • 40
  • 74
  • what is the obj when you alert this? var obj = new window[functionParts[0]](); – Diego Polido Santana Apr 21 '16 at 02:09
  • 1
    It seems like you're probably looking for something like `obj[classFunction]` instead of `obj['classFunction']`, but all of this code is very difficult to follow. I would highly recommend looking into a real module system like Node-style CommonJS modules, and a bundler like Browserify if you need it to run it in the browser. – JMM Apr 21 '16 at 02:30
  • @diego Its an object with functions groupSave and Init – jdog Apr 21 '16 at 02:52
  • @JMM so simple! Thanks, please add an answer I can accept. I will certainly look at better options. – jdog Apr 21 '16 at 02:52
  • Your `groupHandler` function doesn't make a lot sense. `options` is undefined, and you're calling `init` after having `return`ed. What exactly is this code supposed to tell us? – Bergi Apr 21 '16 at 02:54
  • @Bergi thanks for spotting this. I shifted a couple of things around for this question, these mistakes must have happened then. Other modules have the init higher up and options defined. – jdog Apr 21 '16 at 02:58
  • @jdog Ok, thanks, answer added. I shouldn't have said "real" module system, but rather something like established, mainstream, or common. – JMM Apr 21 '16 at 03:01

1 Answers1

1

It's possible that part or all of the problem is this:

return obj['classFunction'];

// ^^ Equivalent to obj.classFunction. In other words, reading a property
// called `classFunction`, not reading a property whose name is the value
// of the `classFunction` variable you set.

I haven't analyzed the code enough to fully understand it, but based on the context it seems that you'd mean this:

return obj[classFunction];
JMM
  • 26,019
  • 3
  • 50
  • 55
  • 1
    Wow, this really seems to make the entire question a dupe of https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets :-) – Bergi Apr 21 '16 at 11:22
  • sorry @bergi - I would have loved to know that earlier as well ;) – jdog Apr 23 '16 at 07:52