function 之 arguments 、call 、apply

來源:互聯網
上載者:User

標籤:結果   callee   style   else   自己   ++   個數   name   針對   

1.arguments

arguments.length為函數實參個數,arguments.callee引用函數自身。

arguments他的特性和使用方法

特性:

arguments對象和Function是分不開的。因為arguments這個對象不能顯式建立,arguments對象只有函數開始時才可用。

使用方法:

雖然arguments對象並不是一個數組,但是訪問單個參數的方式與訪問數組元素的方式相同

例如:

arguments[0],arguments[1],。。。。。。。。arguments[n],

在js中 不需要明確指出參數名,就能訪問它們,例如:

function test() {  var s = "";  for (var i = 0; i < arguments.length; i++) {    alert(arguments[i]);    s += arguments[i] + ",";  }  return s;}test("name", "age")輸出結果:name,age

我們知道每一個對象都有自己的屬性,arguments對象也不例外,首先arguments的訪問猶如Array對象一樣,用0到arguments.length-1來枚舉每一個元素。

下面我們來看看callee屬性,返回正被執行的 Function 對象,也就是所指定的 Function 對象的本文。callee 屬性是 arguments 對象的一個成員,僅當相關函數正在執行時才可用。
callee 屬性的初始值就是正被執行的 Function 對象,這允許匿名的遞迴函式。

var sum = function (n) {  if (1 == n) {    return 1;  } else {    return n + arguments.callee(n - 1);  }}alert(sum(6)); // 21 = 6 + 5 + 4 + 3 + 2 + 1

通俗一點就是,arguments此對象大多用來針對同個方法多處調用並且傳遞參數個數不一樣時進行使用。根據arguments的索引來判斷執行的方法。

2.call

.

3.apply

.

function 之 arguments 、call 、apply

相關文章

聯繫我們

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