嘗試在JavaScript中創建工廠時,當我向工廠傳遞一個值時,我希望它返回正確的字符串而不是整數。從工廠返回正確的值
function Factory(amount){
switch (amount) {
case amount == 1:
return "one "
break;
case amount == 2:
return "two "
break;
case amount > 2:
return "more than two "
break;
}
return amount;
}
var per1 = Factory(1);
console.log(per1, 'per1');
var per2 = Factory(2);
var per3 = Factory(3);
但是,當我退出per1
我得到的值:1
代替'one '
。
我有什麼不完全正確的?
以下是w3schools如何教授它 - > http://www.w3schools.com/js/js_switch.asp –