js-arguments的認識

來源:互聯網
上載者:User

標籤:

js-arguments的認識部落格分類:
  • Web前端-JS語言核心
arguments

zccst



2014-6-10  自己構造一個Array like
類數組的構成要點:
1,元素是數組式索引
2,有length屬性

Js代碼  
  1. var test3 = {  
  2.     0 : 10,  
  3.     1 : function(){  
  4.         return this[0];  
  5.     },  
  6.     length:2  
  7. }  
  8. console.log(Array.prototype.slice.call(test3));//列印[10, function()]  





2014-6-9
函數參數arguments是類數組,但不是數組。(arguments是對象)
如果希望轉成真正的數組,則可以
var slice = [].slice;
slice.call(arguments);
也可以:Aarray.prototype.slice.call(arguments);

Js代碼  
  1. var o = {  
  2.     name:"javascript"  
  3. };  
  4. var f1 = function(){  
  5.     return 2;  
  6. }  
  7. var foo = function(a,b,c){  
  8.     console.log(arguments, typeof arguments, $.type(arguments), typeof arguments.push);  
  9.     //[100, function(), Object { name="javascript"}] object object undefined  
  10.   
  11.     //儘管arguments不是數組,但仍可以通過for迴圈取出每一個元素  
  12.     for (var i in arguments ){  
  13.         console.log(arguments[i]);  
  14.     }  
  15.     /*列印 
  16.         100 
  17.         function() 
  18.         Object { name="javascript"} 
  19.     */  
  20.   
  21.     //現在讓數組的slice運行在arguments的context中,參數為0,則將arguments轉成了數組,(arguments本身沒變)  
  22.     var ret = Array.prototype.slice.call(arguments);  
  23.   
  24.     console.log(ret, typeof ret, $.type(ret), typeof ret.push);  
  25.     //[100, function(), Object { name="javascript"}] object array function  
  26. }  
  27. foo(100,f1,o);  










基礎介紹
js進行函數調用時,除了指定的參數外,還建立一個隱含的對象——arguments。
一、arguments可以用arguments[index]這樣的文法取值,擁有長度屬性length。argumentsObject Storage Service的是實際傳遞給函數的參數,而不局限於函式宣告所定義的參數列表,例如:

Js代碼  
  1. function func(a,b){  
  2.     alert(a);  
  3.     alert(b);  
  4.     for(var i=0;i<arguments.length;i++){  
  5.         alert(arguments[i]);  
  6.     }  
  7. }  
  8. func(1,2,3,4);  


代碼運行時會依次顯示:1,2,1,2,3,4。函數定義了兩個參數,但是在調用的時候傳遞了4個參數。

二、arguments的callee屬性
它表示對函數對象本身的引用,這有利於實現無名函數的遞迴或者保證函數的封裝性。例如:用遞迴來計算1到n的自然數之和:

Js代碼  
  1. var sum=function(n){  
  2.     if(1==n) {  
  3.         return 1;  
  4.     } else {  
  5.         return n + arguments.callee(n-1);  
  6.     }  
  7. }  
  8. alert(sum(100));  


arguments.callee表示引用當前正在執行的函數,或者說是調用arguments.callee的函數對象的引用,它給匿名函數提供了一種自我引用的方式。





更深入,包括了caller, call, apply

Js代碼  
    1. /* 
    2.  * 示範arguments的用法,如何擷取實參數和形數數 
    3.  */  
    4. function argTest(a,b,c,d){  
    5.     var numargs = arguments.length;     // 擷取被傳遞參數的數值。  
    6.     var expargs = argTest.length;       // 擷取期望參數的數值。  
    7.     alert("實參數目為:"+numargs)  
    8.     alert("形數數目為:"+expargs)  
    9.   
    10.     alert(arguments[0])           
    11.     alert(argTest[0])          //undefined 沒有這種用法  
    12. }  
    13. //argTest(1,2)  
    14. //argTest(1,2,3,4,5)  
    15.   
    16.   
    17. /* 
    18.  *  arguments不是數組(Array類) 
    19.  */  
    20.   
    21. Array.prototype.selfvalue = 1;  
    22. function testAguments(){  
    23.     alert("arguments.selfvalue="+arguments.selfvalue);  
    24. }  
    25. //alert("Array.sefvalue="+new Array().selfvalue); // 1  
    26. //testAguments();  //arguments.selfvalue = undefined  
    27.   
    28.   
    29.   
    30. /* 
    31.  * 示範函數的callee屬性. 
    32.  * 說明:arguments.callee:初始值就是正被執行的 Function 對象,用於匿名函數 
    33.  */  
    34. function calleeDemo() {  
    35.     alert(arguments.callee);  
    36. }  
    37. //calleeDemo();  
    38. //(function(arg0,arg1){alert("形數數目為:"+arguments.callee.length)})();  
    39.   
    40.   
    41.   
    42.   
    43. /* 
    44.  * 示範函數的caller屬性. 
    45.  * 說明:(當前函數).caller:返回一個對函數的引用,該函數調用了當前函數 
    46.  */  
    47.   
    48. function callerDemo() {  
    49.     if (callerDemo.caller) {  
    50.         var a= callerDemo.caller.arguments[0];  
    51.         alert(a);  
    52.     } else {  
    53.         alert("this is a top function");  
    54.     }  
    55. }  
    56. function handleCaller() {  
    57.     callerDemo();  
    58. }  
    59.   
    60. //callerDemo();  
    61. //handleCaller("參數1","參數2");  
    62.   
    63.   
    64.   
    65.   
    66.   
    67. /* 
    68.  * 示範apply,call函數的用法 
    69.  * 說明:作用都是將函數綁定到另外一個對象上去運行,兩者僅在定義參數方式有所區別: 
    70.  *       apply(thisArg,argArray); 
    71.  *     call(thisArg[,arg1,arg2…] ]); 
    72.  *     即所有函數內部的this指標都會被賦值為thisArg 
    73.  */  
    74.   
    75.  function ObjectA(){  
    76.     alert("執行ObjectA()");  
    77.     alert(arguments[0]);  
    78.     alert(this.b);//在ObjectB中用call/apply調用ObjectA時,ObjectB的變數b的值。  
    79.     this.hit=function(msg){alert(msg)}  
    80.     this.info="我來自ObjectA"  
    81.  }  
    82.    
    83.  function ObjectB(){  
    84.     alert("執行ObjectB()");  
    85.     this.b = ‘2b‘;  
    86.     //調用ObjectA()方法,同時ObjectA建構函式中的所有this就會被ObjectB中的this替代  
    87.     ObjectA.apply(this,arguments);//ObjectA.call(this);  
    88.     alert(this.info);  
    89.  }  
    90.  //ObjectB(‘參數0‘);  
    91.   
    92.   
    93.  var value="global 變數";  
    94.  function Obj(){  
    95.     this.value="對象!";  
    96.  }  
    97.  function Fun1(){  
    98.     alert(this.value);  
    99.  }  
    100.  //Fun1();  
    101.  //Fun1.apply(window);   
    102.  //Fun1.apply(new Obj());  

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.