Javascript 相当于服务器端的“window[”functionName”](arguments)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8206453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of "window["functionName"](arguments)" in server-side
提问by pars
What is the equivalent code of window["functionName"](arguments)
in NodeJS server-side?
window["functionName"](arguments)
NodeJS服务器端的等效代码是什么?
采纳答案by Mark Kahn
you're looking for global
您正在寻找 global
Note, however, that in modules nothing is ever exposed to this level
但是请注意,在模块中没有任何东西暴露在这个级别
回答by ravi
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!
FWIW!
回答by JerryGoyal
1) If methods are in same js file
1) 如果方法在同一个 js 文件中
define all methods as properties of Handler:
将所有方法定义为 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
2) 如果你想将方法保存在不同的 js 文件中
// Handler.js
module.exports={
application_run: function (name) {
console.log(name)
}
}
Use method defined in Handler.js
in different.js
:
在定义使用方法Handler.js
中different.js
:
// different.js
var methods = require('./Handler.js') // path to Handler.js
methods['application_run']('jerry codes')
Output:jerry codes
输出:杰瑞码
回答by Zeal Nagar
If you want to call a class level function using this
then following is the solution and it worked for me
如果您想使用this
以下方法调用类级函数,则解决方案对我有用
class Hello {
sayHello(name) {
console.log("Hello " + name)
}
callVariableMethod() {
let method_name = 'sayHello'
this[`${method_name}`]("Zeal Nagar!")
}
}
回答by Artur Mirończuk
If You need it in module scope, You can use something like this
如果您在模块范围内需要它,您可以使用这样的东西
var module = require('moduleName');
module['functionName'](arguments);
回答by Artur Mirończuk
Honestly, looking at all these answers they seem a bit too much work. I was playing around to look for other ways around this. You can use the eval()
command to print a variable as text then call it as a function
老实说,看着所有这些答案,他们似乎有点太多工作了。我正在四处寻找解决此问题的其他方法。您可以使用该eval()
命令将变量打印为文本,然后将其作为函数调用
I.e
IE
let commands = ['add', 'remove', 'test'];
for (i in commands) {
if (commands[i] == command) {
var c = "proxy_"+command;
eval(c)(proxy);
}
}
eval(string)(arg1, arg2);
eval(string)(arg1, arg2);
This example script would execute the function proxy_test(proxy)
此示例脚本将执行函数 proxy_test(proxy)
回答by alfadog67
You know, the OP's code inspired me to try this:
你知道,OP 的代码激励我尝试这个:
global.test = function(inVal){
console.log(inVal);
}
global['test']('3 is the value')
But now that I think about it, it's no better than @Ravi' s answer.
但现在我想想,这并不比@Ravi 的回答好。
回答by balyanrobin
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);
});
回答by pradeek
That is not specific to the window object. In JavaScript any property of the object can be accessed this way. For example,
这不是特定于 window 对象的。在 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