繁体   English   中英

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

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

我的主要问题是,function 参数接受什么类型的数据? 我会将比较作为参数传递,例如

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

我正在尝试获取比较表达式而不是结果。

有可能吗? 有什么办法吗? 谢谢

是的,您可以将函数作为参数传递,并且在 JS 和其他语言中传递“谓词”函数是很常见的模式。

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

对的,这是可能的:

 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

更重要的是要了解您可以将任何表达式作为参数传递给 function,包括但不限于:任何原始类型(字符串、数字、boolean、未定义、Z37A6259CC0C1DAE299A7866489DFF0BD 等表达式)像你的情况

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM