使用功能和关闭一起相同的结果,我需要做以下(下同)函数调用给出相同的结果在这两种情况下:如何获得通过JavaScript的
sum(5,4); // 9
sum(5)(4); // this should also print 9
我尝试以下,但它不工作:
function sum(x,y){
var a = x;
var b = y;
if (y == undefined && y == ''){
return function (a,b){
return a +b;
}
}
else {
return a +b;
}
}
有什么建议吗?
this'y == undefi ned && y =='''不能为真 – Hacketo
我没有得到你的问题..你的意思是sum(5)+ sum(4)也应该是9? ..我很困惑 – DTH
使用if(typeof y ==“undefined”)而不是 – DTH