【JS】 arguments參數簡介

來源:互聯網
上載者:User

標籤:arguments js

在讀singalR.js源碼時發現了它裡面的一個format方法:

  format: function () {            /// <summary>Usage: format("Hi {0}, you are {1}!", "Foo", 100) </summary>            var s = arguments[0];            for (var i = 0; i < arguments.length - 1; i++) {                s = s.replace("{" + i + "}", arguments[i + 1]);            }            return s;        },

然後發現,format function裡面並沒有參數,還是直接用arguments接收,後來學習了一下,發現arguments方法很強大,他能直接被調用,不管傳遞幾個參數,他都能接收到,上邊的方法就實現了.NET 裡面 string.Format()方法。以後做參數判斷,可以用arguments.length判斷傳遞的參數數量是不是正確的。

最後,定義變數名不要為arguments,這就好比保留字,所以還是不要衝突的好。上述代碼可以作為工具類裡的一個方法了,簡單粗暴實用。

本文出自 “Pz的技術實訓基地” 部落格,請務必保留此出處http://panzi.blog.51cto.com/4738203/1590380

【JS】 arguments參數簡介

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.