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?

7
  • what is the obj when you alert this? var obj = new window[functionParts[0]](); Commented Apr 21, 2016 at 2: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. Commented Apr 21, 2016 at 2:30
  • @diego Its an object with functions groupSave and Init Commented Apr 21, 2016 at 2:52
  • @JMM so simple! Thanks, please add an answer I can accept. I will certainly look at better options. Commented Apr 21, 2016 at 2:52
  • Your groupHandler function doesn't make a lot sense. options is undefined, and you're calling init after having returned. What exactly is this code supposed to tell us? Commented Apr 21, 2016 at 2:54

1 Answer 1

1
+50

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];
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, this really seems to make the entire question a dupe of stackoverflow.com/questions/4968406/… :-)
sorry @bergi - I would have loved to know that earlier as well ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.