从文本变量调用对象函数

Rya*_*yan 3 javascript

app = {
    echo: function(txt) {
       alert(txt)
    },
    start: function(func) {
        this.func('hello'); 
    }
}

app.start('echo');
Run Code Online (Sandbox Code Playgroud)

我需要调用作为func传递的任何函数.怎么做?这个例子对我不起作用.

rah*_*han 8

this[func]而不是this.func

   app={
          echo:function(txt){
          alert(txt)
          },
         start:function(func){
            this[func]('hello');
          } 
        }

      app.start('echo');
Run Code Online (Sandbox Code Playgroud)