1

I have this simple code in blog1.js:

var blog_validation = (function() {
  var fields = {}
  var email = function() {
    console.log("validating email")
  }

  var add_field = function(field, type) {
    fields[field] = type;
  }

  return {
    fields: fields,
    email: email,
    add_field: add_field
  };
})();

and this in blog2.js:

(function() {
  blog_validation.add_field("test", "email");
  blog_validation.fields['test']();
})();

In the example above i have hard-coded the "test" string for easier troubleshooting instead of writing in a for loop iterating over all the hash values. The email function also doesnt do anything for now, im just going to see if it gets called correctly.

Basically calling the function reference in the hash does not work, it just says that "string is not a function".

From other resources on the net ive seen people save functions in a hash and then call them later like that but i havent seen people storing function references so im not sure if this would work but since you can store functions in a hash i guess you would be able to store a function reference as well ?

Can someone explain why this isnt working ?

6
  • Why downvote ? I provided code examples and everything Commented Mar 29, 2015 at 13:01
  • I didn't downvote, but maybe it would be helpful if you explain at the beginning of your question what you are trying to achieve and why you are doing what you are doing... Commented Mar 29, 2015 at 13:06
  • I guess it should have put the code after the explaination ? It doesnt matter what the code is, its just an example of that im not able to call a function in a hash. Commented Mar 29, 2015 at 13:12
  • I was just guessing why you were downvoted. However, if your title would have been: Why can't I call a function stored in a hash table? or similar, then maybe it would have been easier to understand your problem. Anyway, first explaining your task, then your (not yet working) solution, and finally your problem is usually a good flow for a question. Commented Mar 29, 2015 at 13:19
  • 1
    This also came to my mind ;) But as you can imagine, you gain experience with every question you ask. I bet your next question will be clearer. Anyway, you can still edit this question in order to improve it. This might bring you some more upvotes over time. Commented Mar 29, 2015 at 13:42

1 Answer 1

2

The error is correct. You're adding the string 'email' to test and then trying to run it as a function.

You need to make a small change to your code to call the method using that string:

var add_field = function(field, type) {
  fields[field] = this[type];
}

DEMO

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

1 Comment

Thanks, that works as expected by refering to the function with the this keyword.

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.