feliperohde
feliperohde

Reputation: 45

Call functions from function inside an object in a javascript loop

I have a javascript object with some functions inside, I wish I could call them in a loop, something like this:

funcs: {
  func1: function() {
      return true;
  },
  func2: function() {
      return false;
  }
}

for(func in funcs) {
   console.log(funcs[func]());
   console.log(funcs[func].call());
}

Upvotes: 1

Views: 1053

Answers (1)

Cohars
Cohars

Reputation: 4022

Both work. But the declaration of your object is not correct. It is var object = { /*something*/};

var funcs = {
  func1: function() {
      return true;
  },
  func2: function() {
      return false;
  }
};

for(func in funcs) {
   console.log(funcs[func]());
   console.log(funcs[func].call());
}

Output

true
true
false
false

Upvotes: 2

Related Questions