簡體   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