简体   繁体   English

当我有 function 名称时执行 function 的正确方法

[英]Correct way to execute a function when I have function name

I have a string "Car.run(true)" or "Car.find('window')" , I want to execute the function run or find and also pass respective parameters passed.我有一个字符串"Car.run(true)""Car.find('window')" ,我想执行 function runfind并传递相应的参数。 What is the correct way to do it without using eval ?不使用eval的正确方法是什么?

I tried to split the string, extracted the function name and parameters using regex but issue is with boolean values(it will a string after extraction).我尝试拆分字符串,使用正则表达式提取 function 名称和参数,但问题在于 boolean 值(提取后它将是一个字符串)。

Can anyone guide me to resolve this blocker?谁能指导我解决这个阻止程序?

In javascript land, classes are just objects that can be referenced using dictionary lookup syntax在 javascript 领域,类只是可以使用字典查找语法引用的对象

class Car { run(x) { return x } find(x) { return x }  }
car = new Car()
method_name = 'run'
car[method_name]    # == ƒ run(x) { return x }
car[method_name](1) # == 1

In python land you would need hasattr() and getattr()在 python 土地上,您需要hasattr()getattr()

You can try simply like below, if you dont want to use eval :如果您不想使用eval ,您可以像下面这样简单地尝试:

var functionHolder = "Car.run(true)";
var myTmpFunction = new Function(functionHolder);
myTmpFunction ();  //This would invoke

Generally using eval is not a good idea, and this isn't great either but it's a start:通常使用eval不是一个好主意,这也不是很好,但这是一个开始:

 function Car() { this.run = function(args) { console.log('Running', args) } } const str = "Car.run(true)"; // Get the constructor name and the function with arguments as a string const [ctor, fn] = str.split('.'); // The name of the function without parens const fnName = fn.replace(/\((.+)\)/, '') // Get the argument list of the function const originalArgs = fn.match(/\((.+)\)/) // Clean arguments const args = originalArgs[1].split(',').map(str => str.trim()).filter(Boolean); // Instantiate a new object based on the name const f = new(Function.prototype.bind.apply(window[ctor]))(); // Invoke the function with the arguments f[fnName].apply(f, args) // Running true

General idea comes from the AngularJS source and how it instantiates objects from strings.总体思路来自AngularJS 源以及它如何从字符串实例化对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我将其名称作为字符串时执行 JavaScript 函数 - 不工作 - Execute a JavaScript function when I have its name as a string - Not working 当我将其名称作为字符串时如何执行 JavaScript function - How to execute a JavaScript function when I have its name as a string 如何执行 JavaScript Function 当我将其名称作为字符串时(使用具有重载参数的参数) - How to execute a JavaScript Function When I have its name as a string (with parameters that have an overload argument) 当我将其名称作为字符串时,如何执行私有JavaScript函数 - How to execute a private JavaScript function when I have its name as a string 我可以命名一个 JavaScript function 并立即执行吗? - Can I name a JavaScript function and execute it immediately? 当其名称作为字符串传递给函数时,如何执行JavaScript函数? - How to execute a JavaScript function when its name is passed as a string in function? 我有一个检测2格之间碰撞的功能。 碰撞结果为真时如何执行新功能? - I have a function detecting collision between 2 divs. How do I execute a new function when the collision result is true? 当我在 javascript 中按下前进键或后退键时,我需要一种执行特定功能的方法 - I Need a way to execute a particular function when i press the forward key or the back key in javascript 当我不带参数动态将其名称作为字符串发送时,如何执行javascript函数 - how to execute javascript function when i send its name as string dynamically with out parameters 我可以将函数名称作为数组变量吗? - Can I have a function name be an array variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM