3 回答

我愛吃牛排
TA貢獻47條經(jīng)驗 獲得超127個贊
function?cal(a,b){ if?(b?===?undefined)?{ return?2?*?Math.PI?*?a;? }else{ return?2?*?(a?+?b);? } } console.log(cal(1)); console.log(cal(1,2));

ruibin
TA貢獻358條經(jīng)驗 獲得超213個贊
傳入兩個參數(shù)就行。例如
function calc(rect, circle) {
? if (rect) {
? ? ?if (rect.calcType === 'area') {
? ? ? ? return rect.height * rect.width;
? ? ?} else {
? ? ? ? return 2 * (rect.height + rect.width);
? ? ?}
? } else {
? ? ?if (circle.calcType === 'area') {
? ? ? ? return (circle.r *circle.r) * Math.PI / 2;
? ? ?} else {
? ? ? ? return 2 * Math.PI * circle.r;
? ? ?}
? }
}
console.log(calc({width: 20, height: 30, calcType: 'area'}));
console.log(calc(null, {r: 5, calcType: 'Perimeter'}));
添加回答
舉報
0/150
提交
取消