0

I have the below code.

(function($) {

    var myFunction = function(element) {

        var myCallerFunction = function() {
            var functionName = 'myInternalCallFunction';
            myFunction[functionName]();
            console.log(2);

        }

        var myInternalCallFunction = function() {

            console.log(1);
        }

        myCallerFunction();

    }

    $.fn.myfunction = function(options) {
        var func = new myFunction();
    }

})(jQuery);

Inside myCallerFunction I created a variable which holds the function name I want to call. I then try to call it, however it returns that it can't find the function. It finds the namespace myFunction okay as if I change:

myFunction[functionName]();

to

myFunctionTest[functionName]();

It returns it can't find "myFunctionTest" instead of not being able to find "myInterncalCallFunction".

Any ideas why it can't find the function?

5
  • You are defining myInternalCallFunction too late. It simply doesn't exist yet (and isn't a property of myFunction) Commented Nov 8, 2012 at 20:36
  • 3
    You can't retrieve local variables dynamically, they have to be properties of some objct. And myInternalCallFunction is not a property of myFunction btw. Commented Nov 8, 2012 at 20:36
  • Take a look now, I amended it - incorrect code was posted before! I'm just calling $().myfunction() and in Firebug it comes up with "TypeError: myFunction[functionName] is not a function" Commented Nov 8, 2012 at 20:40
  • @Ashley yes, because myFunction[functionName] is actually undefined.. did you read my comment? Commented Nov 8, 2012 at 20:44
  • Here: myFunction[functionName] functionName is being called as a property of an Object instead of a parameter of the function for what I can see. Have you tried wrapping everything up in an object and calling the properties? Commented Nov 8, 2012 at 20:59

1 Answer 1

1

By calling myFunction['myInternalCallFunction']() you are actually calling myFunction.myInternalCallFunction(). Of course, it is undefined since myInternalCallFunction is not a property of myFunction, it is its private variable. One way of solving this while not turning private variables to properties:

(function($) {

    var myFunction = function(element) {
        var cache = {};
        var myCallerFunction = function() {
            var functionName = 'myInternalCallFunction';
            cache[functionName]();
            console.log(2);

        }

        cache.myInternalCallFunction = function() {

            console.log(1);
        }

        myCallerFunction();

    }

    $.fn.myfunction = function(options) {
        var func = new myFunction();
    }

})(jQuery);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.