Miko Diko
Miko Diko

Reputation: 954

Using Javascript's Hash-Table's value to call a function

I basically want to below code to work, is there a way ?

var hash_table = new Object();
hash_table['a'] = foo;
alert(hash_table['a'](1)); // 1 is just a simple parameter for example.
                           // this line should print "2" in alert();.


function foo(params) {
    alert("params: " + params); // just simple print in alert(); (will print 1)
    return 2;
}

Upvotes: 0

Views: 237

Answers (2)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

You are defining foo after using it. Make sure you define foo first then use it.

So,

function foo(params) {
    ...
}

Should come before

hash_table['a'] = foo;

Upvotes: 1

Dan D.
Dan D.

Reputation: 74685

There is nothing wrong with that code. The code does what you describe it should do.

Upvotes: 0

Related Questions