代码之家  ›  专栏  ›  技术社区  ›  sunwarr10r

使用列表中的函数名进行函数链接[重复]

  •  0
  • sunwarr10r  · 技术社区  · 8 年前

    它的等效代码是什么 window["functionName"](arguments)

    0 回复  |  直到 6 年前
        1
  •  14
  •   None    14 年前

    你在找什么 global

    但是,请注意,在模块中,没有任何内容会暴露于此级别

        2
  •  125
  •   ravi    12 年前

    var x = { }; // better would be to have module create an object
    x.f1 = function()
    {
        console.log('Call me as a string!');
    }
    

    现在,在模块中,可以使用字符串中的值调用它:

    var funcstr = "f1";
    x[funcstr]();
    

    我自己在学绳子,上面可能是各种各样的错误:-)。或许编写这个示例的更好方法是(对于模块m.js):

    module.exports =
    {
        f1: function() { console.log("Call me from a string!"); },
        f2: function(str1) { this[str1](); }
    }
    

    现在您可以:

    var m = require('m.js');
    m.f2('f1');
    

    var m = require('m.js');
    m['f1']();
    

    FWIW!

        3
  •  13
  •   Brice Miramont GorvGoyl    7 年前

    1) 如果方法在同一个js文件中

    var Handler={};
    
    Handler.application_run = function (name) {
    console.log(name)
    }
    

    现在就这样说吧

    var somefunc = "application_run";
    Handler[somefunc]('jerry codes');
    

    输出: 杰瑞代码


    2) 如果您想在不同的js文件中保留方法

    //    Handler.js
    module.exports={
        application_run: function (name) {
            console.log(name)
        }
    }
    

    使用中定义的方法 Handler.js different.js

    //    different.js
    var methods = require('./Handler.js')   // path to Handler.js
    methods['application_run']('jerry codes')
    

    杰瑞代码

        4
  •  5
  •   Zeal Nagar    7 年前

    如果要使用调用类级函数 this 下面是我的解决方案

    class Hello {
      sayHello(name) {
        console.log("Hello " + name)
      }
      callVariableMethod() {
        let method_name = 'sayHello'
        this[`${method_name}`]("Zeal Nagar!")
      }
    }
    
        5
  •  4
  •   Artur Mirończuk    9 年前

    如果您在模块范围内需要它,您可以使用如下内容

    var module = require('moduleName');
    
    module['functionName'](arguments);
    
        6
  •  4
  •   user8979341 user8979341    8 年前

    eval() 命令将变量打印为文本,然后将其作为函数调用

    let commands = ['add', 'remove', 'test'];
    for (i in commands) {
        if (commands[i] == command) {
            var c = "proxy_"+command;
            eval(c)(proxy);
        }
    }
    

    eval(string)(arg1, arg2);

    proxy_test(proxy)

        7
  •  1
  •   alfadog67    7 年前

    你知道,OP的代码启发了我去尝试:

    global.test = function(inVal){
        console.log(inVal);
    }
    
    global['test']('3 is the value')
    

    但现在我想起来了,这并不比@Ravi的答案更好。

        8
  •  0
  •   balyanrobin    10 年前

    var _ = require('lodash');
    var fnA1 = require('functions/fnA1');
    var fnA2 = require('functions/fnA2');
    
    module.exports = {
        run: function(fnName, options, callback) { 
            'use strict';
            var nameSpace = fnName.toString().split('.');
            // if function name contains namespace, resolve that first before calling
            if (nameSpace.length > 1) {
                var resolvedFnName = this;
                _.forEach(nameSpace, function(name){
                    resolvedFnName = resolvedFnName[name];
                });
                resolvedFnName(options, callback);
            } else {
                this[fnName](options, callback);
            }
        },
        fnA1: fnA1,
        fnA2: fnA2
    };
    

    把这个叫做

    importVariable.run('fnA1.subfunction', data, function(err, result){
        if (err) {return callback(err);}
        return callback(null, result);
    });
    
        9
  •  -2
  •   pradeek    14 年前

    var test = {
        prop1 : true
    };
    
    console.log(test.prop1); // true
    console.log(test["prop1"]); // also true
    

    阅读更多信息: https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects