通过node.js上的(string)名称调用函数

时间:2022-03-09 13:33:54

At the client side you do it via window["functionName"](arguments);. How it would be possible in node.js server-side code?

在客户端,您可以通过窗口[“functionName”](参数)进行操作;如何在节点中实现。js服务器端代码?

6 个解决方案

#1


11  

you're looking for global

你正在寻找全球

Note, however, that in modules nothing is ever exposed to this level

但是,请注意,在模块中,没有任何东西公开到这个级别

#2


91  

If you need such a capability within a module, one hack is to store such module functions in variables within the module and then call them by accessing them from the module object properties. Example:

如果在模块中需要这样的功能,一种方法是将这些模块函数存储在模块中的变量中,然后通过从模块对象属性访问它们来调用它们。例子:

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

Now, within the module, you can call it using the value from a string:

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

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

I am learning the ropes with Node myself, the above is probably all sorts of wrong :-). Perhaps a marginally better way to write this example would be (for the module m.js):

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

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

Now you can:

现在您可以:

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

Or even just:

甚至是:

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

FWIW!

就其价值而言!

#3


2  

If You need it in module scope, You can use something like this

如果在模块范围内需要它,可以使用类似的东西

var module = require('moduleName');

module['functionName'](arguments);

#4


1  

1) If methods are in same js file

define all methods as properties of Handler:

将所有方法定义为处理程序的属性:

var Handler={};

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

Now call it like this

现在这样称呼它

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

Output: jerry codes

输出:杰瑞代码


2) If you want to keep methods in a different js file

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

Use method defined in Handler.js in different.js:

使用处理程序中定义的方法。js different.js:

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

Output: jerry codes

输出:杰瑞代码

#5


0  

I use this for node, see if this approach works for you

我在节点上使用这个方法,看看这个方法是否适合您。

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
};

call this like

把这个像

importVariable.run('fnA1.subfunction', data, function(err, result){
    if (err) {return callback(err);}
    return callback(null, result);
});

#6


-1  

That is not specific to the window object. In JavaScript any property of the object can be accessed this way. For example,

这不是特定于窗口对象的。在JavaScript中,对象的任何属性都可以通过这种方式访问。例如,

var test = {
    prop1 : true
};

console.log(test.prop1); // true
console.log(test["prop1"]); // also true

Read more here : https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects

在这里阅读更多:https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects

#1


11  

you're looking for global

你正在寻找全球

Note, however, that in modules nothing is ever exposed to this level

但是,请注意,在模块中,没有任何东西公开到这个级别

#2


91  

If you need such a capability within a module, one hack is to store such module functions in variables within the module and then call them by accessing them from the module object properties. Example:

如果在模块中需要这样的功能,一种方法是将这些模块函数存储在模块中的变量中,然后通过从模块对象属性访问它们来调用它们。例子:

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

Now, within the module, you can call it using the value from a string:

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

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

I am learning the ropes with Node myself, the above is probably all sorts of wrong :-). Perhaps a marginally better way to write this example would be (for the module m.js):

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

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

Now you can:

现在您可以:

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

Or even just:

甚至是:

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

FWIW!

就其价值而言!

#3


2  

If You need it in module scope, You can use something like this

如果在模块范围内需要它,可以使用类似的东西

var module = require('moduleName');

module['functionName'](arguments);

#4


1  

1) If methods are in same js file

define all methods as properties of Handler:

将所有方法定义为处理程序的属性:

var Handler={};

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

Now call it like this

现在这样称呼它

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

Output: jerry codes

输出:杰瑞代码


2) If you want to keep methods in a different js file

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

Use method defined in Handler.js in different.js:

使用处理程序中定义的方法。js different.js:

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

Output: jerry codes

输出:杰瑞代码

#5


0  

I use this for node, see if this approach works for you

我在节点上使用这个方法,看看这个方法是否适合您。

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
};

call this like

把这个像

importVariable.run('fnA1.subfunction', data, function(err, result){
    if (err) {return callback(err);}
    return callback(null, result);
});

#6


-1  

That is not specific to the window object. In JavaScript any property of the object can be accessed this way. For example,

这不是特定于窗口对象的。在JavaScript中,对象的任何属性都可以通过这种方式访问。例如,

var test = {
    prop1 : true
};

console.log(test.prop1); // true
console.log(test["prop1"]); // also true

Read more here : https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects

在这里阅读更多:https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects