如何使用变量声明中的匿名函数将字符串赋值给变量?

Tod*_*nce 0 javascript anonymous-function

var destURL = function () {
            if (this.typeOfDash == 'edit') {
                return '../../ajax/getParams.aspx';
            }
            else {
                return 'DashCreator.aspx';
            }
Run Code Online (Sandbox Code Playgroud)

}

我希望这会工作,但它只是将函数分配给变量...我希望得到返回值作为变量值...任何人?

Kei*_*amo 6

您可以立即评估函数以获取返回值.

var destURL = function () {
            if (this.typeOfDash == 'edit') {
                return '../../ajax/getParams.aspx';
            }
            else {
                return 'DashCreator.aspx';
            }
}()
Run Code Online (Sandbox Code Playgroud)