简体   繁体   English

我可以将比较作为 function 参数传递吗?

[英]Can I pass a comparison as a function parameters?

My main question is, what type of data function parameters is accepting?我的主要问题是,function 参数接受什么类型的数据? I would pass a comparison as parameters as for exemple我会将比较作为参数传递,例如

    Function test(a) //where A is a comparison so for example
test("a" == "a")

I'm trying to get the comparison expression and not the result.我正在尝试获取比较表达式而不是结果。

Is it something possible?有可能吗? Any way to do it?有什么办法吗? Thanks谢谢

Yes, you can pass functions as parameters, and it's quite a common pattern in both JS and other languages to pass "predicate" functions.是的,您可以将函数作为参数传递,并且在 JS 和其他语言中传递“谓词”函数是很常见的模式。

 let isGreaterThan5 = v => v > 5; let out = [1,3,5,7,9].filter(isGreaterThan5); console.log(out);

Yes it is possible:对的,这是可能的:

 function isPosible(param) { // Here you define a function return param; } // Below you are executing the function and printing the result console.log(isPosible("a === a")); // Returns the whole expression

More important is to understand that you can pass any expression as argument to a function, including but not limited to: Any primitive type (string, number, boolean, undefined, null, etc..), functions (callbacks), expressions that evaluates something like your case .更重要的是要了解您可以将任何表达式作为参数传递给 function,包括但不限于:任何原始类型(字符串、数字、boolean、未定义、Z37A6259CC0C1DAE299A7866489DFF0BD 等表达式)像你的情况

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

相关问题 我可以将参数传递给使用_.lodash去抖动的函数吗? - Can I pass parameters to a function debounced with _.lodash? 我可以将一个函数的参数(req、res、next)传递给另一个函数吗? - Can I pass the parameters (req, res, next) of a function to another function? 如何在不使用函数参数的情况下将值传递给函数? - How can I pass a value into a function without using function parameters? 如何将对象参数传递给反引号中的onclick函数 - How can I pass object parameters to the onclick function in backtick 我无法使用addEventListener Javascript / Jquery将参数传递给函数 - I can not pass parameters to a function using addEventListener Javascript/Jquery 我可以将搜索框值作为参数传递给函数吗? - Can I pass my search box values as parameters in my function? js sort()自定义函数如何传递更多参数? - js sort() custom function how can i pass more parameters? 如何在反应中使用导航 function 将参数传递给路由? - How can I pass parameters to route with navigate function in react? 我如何将参数传递给函数以获取当前样式 - how can i pass a parameters to a function to get current Style 我如何将参数传递给点击事件列表内的 function - How can i pass parameters to a function inside click eventlistner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM