views:

56

answers:

2

Hi all,

Is it possible to execute function this way:

this.values(value);

not

this.values[value]();
this.values[value].call();

If so, how? Or any other methods? Thank you.

Here is my code:

write: function(value) {

  this.values = {
    "red": function() { /* do something */ },
    "orange": function() { /* do something */ },
    "blue": function() { /* do something */ }
  };

  return this.values[value]();

}

write("red");
+3  A: 

May be you could use a var inside:

write: function(value) {

  var values = {
    red: function() { /* do something */ },
    orange: function() { /* do something */ },
    blue: function() { /* do something */ }
  };

  return values[value];

}

And return the function instead of running it inside. And call it after.

Mic
Best to put the keys (red, orange, blue) in quotes in case you accidentally use a keyword.
Phil H
@Phil H, you're right, It's a bad habit, I find it easier to read.
Mic
A: 

No. But there isn't anything wrong with how its done at the moment.

Matt