js中的连接函数名称 [英] Concatenation function name in js

查看:30
本文介绍了js中的连接函数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法连接一个js函数名?现在我有这个:

Is there any way to concat a js function name? right now I have this:

switch (playId) {
        case "11":
            play11();
            break;
        case "22":
            play22();
            break;
        case "33":
            play33();
            break;
        case "44":
            play44();
            break;
        default:
            break;
    }

我想做这样的事情:

var function = "play" + playId + "()";

并称之为..如果可能,最好的方法是什么?

and call it.. what is the best way if possible?

解决方案:

我在thefourtheyexdazz 的帮助下所做的非常简单:

What I did with the help of thefourtheye and xdazz is very simple:

var playFunction = window["play" + playId];
    playFunction();

推荐答案

如果这些函数定义在全局范围内,那么你可以从全局对象中获取它们:

If these functions are defined in global scope, then you could get them from the global object:

var foo = window["play" + taskId];

否则,您可以像建议的另一个答案一样将它们放在一个对象中.

Otherwise you could put them together in an object like another answer suggested.

这篇关于js中的连接函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆