js 函數參數 arguments[0]

來源:互聯網
上載者:User

標籤:

 function box() {
            return arguments[0] + ‘|‘ + arguments[1];
           
        }
        alert(box(1, 2, 3, 4, 5, 6));

 

輸出:1|2

說明:1 ECMAScript 函數不介意傳遞進來多少參數,也不會因為參數不統一而錯誤。實際上,
函數體內可以通過 arguments 對象來接收傳遞進來的參數。

     2 arguments 對象的 length 屬性可以得到參數的數量。

 

擴充:

我們可以利用 length 這個屬性,來智能的判斷有多少參數,然後把參數進行合理的應用。
比如,要實現一個加法運算,將所有傳進來的數字累加,而數位個數又不確定。
function box() {
var sum = 0;
if (arguments.length == 0) return sum;
//如果沒有參數,退出
for(var i = 0;i < arguments.length; i++) {
//如果有,就累加
sum = sum + arguments[i];
}
return sum;
//返回累加結果
}
alert(box(5,9,12));

js 函數參數 arguments[0]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.