[英]How to await multiple calls of the same function with different params in 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.