Search code examples
javascriptarraysannyang

Annyang addCommands


I want to make annyang smarter. So i decide to add Commands with the help of users.

for reminders annyang commands are init like this :

var commands = {"your speech":yourFunction}
annyang.addCommands(commands);

With a form I retrive what the user wanted to say and the function associated.

speech = "my speech";
myfunction = "mySuperFunction";

but when i want to add this function like :

newCommand={speech:myfunction};
annyang.addCommands(newCommand);

Annyang says : Command successfully loaded: speech

when i debug newCommand i get : Object {speech: "mySuperFunction"}

See the problem ? JS interpret the function name instead of the string it contains.

Can you help me ? thanks !


Solution

  • You need a create an empty object first, and then you can use array syntax to set it.

    newCommand = {}; //empty object
    speech = "my speech";
    myfunction = "mySuperFunction";
    
    newCommand[speech] = myfunction;
    
    annyang.addCommands(newCommand);