深入理解javascript函數參數

來源:互聯網
上載者:User

標籤:形參   數組   null   預設   傳參   info   區分   color   具名引數   

arguments

javascript對參數要求很隨意,她才不管你傳進來的參數是什麼資料類型,甚至可以不傳參數。實際上,javascript函數調用甚至不檢查傳入形參的個數。

1     function add(x){2         return x + 1;3     }4     console.log(add(1));  //25     console.log(add(‘1‘)); // 116     console.log(add());  // NAN7     console.log(add(1,2,5));  //2

同名形參

非strict 模式下函數可以有同名形參,但之鞥呢訪問最後出現的那個。。。

    function add(x,x,x,){        return x;    }    console.log(add(2,5,6)); //6

參數個數

case1:實參比形參少? 那剩下的形參都設定為undefined

1     function add(x,y){2         console.log(x,y)3     }4     add(1); // 1,undefined

這時候我們可以給y設定一個合理的預設值

1     function add(x,y){2         y = y || 100003         console.log(x,y);4     }5     add(1); //1 10000

case2:形參比實參多? 多了就是廢的! 可以通過arguments[i]拿到。

參數再內部是用一個數組表示的,arguments對象並不是Array的執行個體,它是一個類數組對象。

1     function add(){2         console.log(arguments[0],arguments[1],arguments[2]); //1,2,33         console.log(arguments[0]+arguments[1]); //34         console.log(arguments.length); //45     }6     add(1,2,3,4);

case3:形參和實參一樣多?  此時具名引數和對應arguments對象的值相同,但不是相同的命名空間。換句話說,兩者值同步,命名空間獨立。

 

callee

arguments對象有個callee屬性,是一個指標,指向擁有這個arguments對象的函數,看如下階乘

   function factorial(num){        if(num <= 1){            return 1;        }else{            return  num * factorial(num - 1);        }    }    factorial(6);  //720
1     function factorial(num){2         if(num <= 1){3             return 14         }else{5             return num * arguments.callee(num -1);6         }7     }8     factorial(6); //720

 

caller

函數的caller屬性儲存著調用當前函數的函數的應用,如果是在全域作用於中調用當前函數,它的值是null

下面這個沒撒意義,好玩而已

1     function mother(){2         son();3     }4     function son(){5         mother();6     }7     mother();   //Uncaught RangeError: Maximum call stack size exceeded8     //棧溢出。。。

這個才是正道

1     function mother(){2         son()3     }4     function son(){5         console.log(son.caller);6     }7     mother();  //function mother(){son()}

arguments對象的caller始終是undefined,這樣定義時為了和函數的caller區分開。。

1     function mother(x){2         console.log(arguments.caller);3     }4     mother(100); //undefined

參數傳遞

case1:基本類型值

1 function addTen(num){2 num += 10;3 return num;4 }5 var count = 20;6 var result = addTen(count);7 console.log(count);//20,沒有變化8 console.log(result);//30

case2:參考型別值

在向參數傳遞參考型別的值時,會把這個值在記憶體中的地址複製給一個局部變數,因此這個局部變數的變化會反應在函數的內部(好特麼拗口!!)

1     function setInfo(obj){2         obj.name = ‘lihong‘3     }4     var person = new Object();5     setInfo(person);6     console.log(person.name); //lihong

當在函數內部重寫參考型別的形參時,這個變數引用的就是一個局部對象了。這個局部對象會在函數執行完畢後立即被銷毀

    function setInfo(obj){        obj.name = ‘lihong‘;        console.log(person.name);        obj = new Object();        obj.name = ‘linyao‘;        console.log(person.name);    }    var person = new Object();

運行結果:“老公” “老公”   說明了什嗎?  婆娘不重要!哈哈

 

深入理解javascript函數參數

聯繫我們

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