Decrypter
Decrypter

Reputation: 3000

Pass a function reference that has a variable as its name

I have the following javascript method:

function 123_test_function(){

}

The function is generated by java and sent to the client. The 123 is the id of the component so it could change. i.e I can have another function called 111_test_function() I want to pass this function as a reference.

So I need to create the reference

var 123_test_function = function 123_test_function(){

}

In another js file inside an object I have a function that needs to use the 123_test_function reference like so:

useFunction(123_test_function);

The problem I'm having is which the 123 part of the function. In this object I have a variable(uniqueID) which has the number at the beginning of the function. I need the function call to be something like:

useFunction(uniqueID+"_test_function");

This doesn't seem to pass a function instead it passes a string. Am I doing something wrong?

Upvotes: 0

Views: 120

Answers (4)

DaveRandom
DaveRandom

Reputation: 88647

As a couple of people have correctly pointed out, a function (or indeed variable) name cannot begin with a numeric. Also this syntax is wrong:

var 123_test_function = function 123_test_function(){
} 

The correct syntax would be:

var 123_test_function = function() {
};

...although it should also be noted that the effect of this is exactly the same as a "traditional"

function 123_test_function() {
}

...declaration, in the context of the window object - since window is effectively the global scope of a JS environment in a browser, it doesn't matter how you define the functions, they will always be accessible from anywhere. Understanding exactly what each method of declaring a function means in Javascript is important - luckily, Douglas Crockford to the rescue once again...

People have suggested various methods for calling your named functions from the context of a string, which is basically attempting to use "variable variable" syntax, a subject that has been discussed on SO and elsewhere at length. The eval() approach should be avoided wherever possible - if you find yourself needing an eval() chances are you went wrong somewhere a while back. @Tomalak has the right idea with a collection of functions held in an object, but this still needs the slightly messy string approach to reference things that are actually being accessed by a numeric ID. The collection approach has the advantage of not cluttering up the window object with what are likely to be single/zero use members.

But the way I see it, all you actually need here is an indexed array of functions, where all you need is the numeric index in order to access them. I suggest you create your functions like this:

// Do this once at the top of your JS
var test_functions = [];

// Now, for each function you define:
test_functions[123] = function() {
  // Do stuff here
};

// And when you need to call the functions:
var funcId = 123;
test_functions[funcId]();

Upvotes: 0

Tomalak
Tomalak

Reputation: 338208

For one, identifiers (such as function names) cannot begin with a digit.

To solve your problem, use an object, like this:

// 1. define an object to hold all your functions
var allFunctions = {};

// 2. store any function with a unique string as the ID
allFunctions['123_test_function'] = function () {
  // whatever
};

// 3. call the function
allFunctions['123_test_function']();
allFunctions[uniqueID + '_test_function']();

Objects are associative arrays. They store key/values pairs, so they do exactly what you want here.

Note that functions don't need a name in JavaScript, so I did not use on in step 2.

Upvotes: 3

dhinesh
dhinesh

Reputation: 4764

function test_function(number){
   if(number == 1)
   {
     return function() {}
   }
   if(number == 2)
   {
     return function() {}    
   }
}

call the function like this

var func = test_function(1)
func();

Upvotes: 0

Xion
Xion

Reputation: 22770

If the function is defined as global one, it will be a member of global object (window in case of browsers). Hence you can just do window['id_'+uniqueID+'_test_function'] to access your function

useFunction(window['id_'+uniqueID+'_test_function'])

(Identifiers cannot begin with numbers in JavaScript so I added the 'id_' prefix. You can of course change it to your liking.)

Upvotes: 0

Related Questions