簡體   English   中英

帶有不同調用的Javascript函數匯總

[英]Javascript function summ with different calls

我應該如何使該功能與不同的調用一起使用?

查看警報中的呼叫:

 function summ(a){ return function(b){ return a+b } } alert(summ(5)(10)) //work alert(summ(5,10)) // not work 

 function summ(a,b){ if (typeof b !== "undefined") { // check if b is present. return a+b; } else { // b is not there, return another function that `capture` a return function(b){ return a+b; } } } alert(summ(5)(10)) // work alert(summ(5,10)) // work 

基本上,您想要的函數summ必須必須處理兩種不同的情況。

如果b不存在,你的函數需要返回嵌有一個值的另一個功能a

如果存在b ,則它將a+b作為正常函數返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM