1

In JavaScript I have associative array of functions like this (like static field of a class):

functions={'name': function(){},'name':function(){}};

each function do something with array. I also have method:

this.doSomethind= function(name, array){//class member

I need to call one of the functions finding it by name.

$.each(functions, function(key, value) {
                 if(key == name)
                    //here I need to call function with array as paremeter. It seems value(array); doesn't work.
                        }); 
}

Sorry if it's a dumb question, I'm just new in JavaScript. Thanks

3 Answers 3

5

If I'm understanding your problem correctly, you should just be able to say:

functions[name](array);

Instead of the $.each() loop.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

value.apply(this, [array])

Comments

0

As far as the information you have provided, you cannot access a parameter that is not yet in scope from the class methods. in your example spelling corrected

  //'array' is undefined / out of scope
  this.doSomething = function(name,array){
    //array is defined / in scope!
   };

Otherwise if the variable array you are talking about is defined in the scope somewhere, the first answer will work.

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.