1

I am trying to execute a function from each object key here is the code.

var testObj = { text: "this is text" , alltext: "this is all text" };

function text() {
    alert("this is test function for first value");
}

function alltext() {
    alert("this is test function for first second!");
}

for (item in testObj) { 
    console.log(item);
    item();
}

This gives me that item not a function.

Please visit this link to see the original code. I am using Backbone.js to create a form.The code is commented.

http://vianx.com/tst/script2.js

in this case gives that the "fieldConstructor" in not a function.

4
  • 7
    Well, item() isn't a function. Commented Feb 9, 2016 at 21:04
  • Only solution in your case, without assigning those functions as members of an object... is eval. Commented Feb 9, 2016 at 21:06
  • You could grab the field, and use eval to call a function. Commented Feb 9, 2016 at 21:06
  • Put your funcs in an object like var funcs = {text:function(){},alltext:function(){}}; then do funcs[item]() Commented Feb 9, 2016 at 21:43

5 Answers 5

0

So I believe you are mistaken about how a dictionary works in javascript. The first value is a key and will always be a string value to do what you want you can do this

var testObj = [[text, "this is text"] , [alltext, " thi is all text" ]];

function text () {
  alert( "this is test function for first value");
}

function alltext () {
  alert( "this is test function for first second!" );
}


for ( index in testObj ) {
   
  console.log(index);
  testObj[index][0]();
}

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

2 Comments

I have used the underscore "pair" object method to convert the object into array "key,value" pairs.Then I can execute them. _.pairs({one: 1, two: 2, three: 3}); => [["one", 1], ["two", 2], ["three", 3]]
@Jack Mata, please do not indent the tags for code snippets (just the tags). it makes funny renderings.
0

You should use JavaScript eval function:

for (item in testObj) {
    console.log(testObj[item]);
    eval(item + "()");
}

3 Comments

Is there is anyway to avoid eval ?? Because it has a security risk?
It depends… A malicious user is potentially able to execute any client-side script he wants with his browser's Developer Tools. Remember that these scripts will be running on his client, not on your server. To learn more, read this: stackoverflow.com/questions/1651118/…
@codeforce It's only a security risk if the contents of testObj can be manipulated outside the control of the user.
0

Or you could reference the functions from within your object

var testObj = { text: text , alltext : alltext };

function text() {
  alert( "this is test function for first value");
}

function alltext() {
  alert( "this is test function for first second!" );
}


for ( item in testObj ) {

  console.log(item);
  testObj[item]();
}

1 Comment

I can not because I have a data from outside source as key value pairs.
0

Construct testObj is like this:

var testObj = { 
    text: function(){alert("this is test function for first value")} ,
    alltext : function(){alert("this is test function for first second!")} 
};

Then simply call testObj.a();

4 Comments

You mean testObj[item]. You have to use [] notation to access an object property dynamically.
No, that will look for a property literally named item, not use the value of the item variable.
I would argue. They are quite the same, except that the square bracket operator can access object's members using a string variable, whereas dot operator can only use literals. Please, take a look at: /questions/4968406/javascript-property-access-dot-notation-vs-brackets
That's exactly what I'm saying. testObj.a() looks for a literal property named a, it doesn't use the value of the variable a. If the property name is in a string variable, you have to use square brackets.
0

You can do it by attaching the dynamic functions to the window object.

var testObj = { text: "this is text", alltext: " thi is all text" };

for (var item in testObj) {
    window[item] = function () {alert("hello world")};
}

console.log(window['text'],window['alltext']);

window.text();
window.alltext();

The functions can also be called without using "window." i.e.:

text();
alltext();

Or if you are wanting to call a function by property name dynamically...

var testObj = { text: "this is text", alltext: " thi is all text" };

window['text'] = function() {
    alert("this is test function for first value");
}

window['alltext']  = function (){
    alert("this is test function for first second!");
}

for (var item in testObj) {
    window[item]();// = function () {alert("hello world")};
}

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.