0

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;
}
1
  • What's wrong with your code? I have the expected behaviour: two alerts, the first with "params: 1" and the second with "2". Commented Sep 20, 2013 at 6:21

2 Answers 2

1

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

1 Comment

The Miko Diko code should be working as well, because the function declaration is hoisted to the top of the scope.
0

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

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.