[英]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.