搞清arguments,callee,caller

arguments是什麼?

arguments是函數調用時,建立的一個相似的數組但又不是數組的對象,而且它存儲的是實際傳遞給函數的參數,並不侷限於函數聲明的參數列表哦。html

尼瑪,什麼意思?chrome

寫個demo看看,代碼見下數組

<!DOCTYPE html>
    <head>
        <title>arguments</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
       <script>
           function obj(){
               //利用instanceof判斷arguments
               console.log( 'arguments instanceof Array? ' + (arguments instanceof Array) );
               console.log( 'arguments instanceof Object? ' + (arguments instanceof Object) );
               console.log(arguments);
           }
           obj();
       </script>
    </body>
</html>

運行該代碼,經過chrome調試器,可得下圖閉包

 

我利用instanceof判斷arguments,從打印的效果看,arguments是一個對象。函數

而後展開打印出的arguments,能夠從上圖得知,它裏面包括了許多屬性,callee也在內。ui

接下來,咱們修改上面的代碼,在調用obj函數時,給它傳遞參數,但obj函數是沒有參數的。spa

具體代碼見下3d

<!DOCTYPE html>
    <head>
        <title>arguments</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
       <script>
           function obj(){
               console.log( 'arguments instanceof Array? ' + (arguments instanceof Array) );
               console.log( 'arguments instanceof Object? ' + (arguments instanceof Object) );
               console.log(arguments);
           }
           //向obj傳遞參數
           obj('monkey','love',24);
       </script>
    </body>
</html>

經過chrome調試器,可得下圖調試

 

你們能夠看見,arguments包含了三個咱們給它傳遞的參數」monkey」,」love」,24。code

因此說,爲何arguments是存儲的實際傳遞給函數的參數呢,而不是函數聲明的參數。

callee是什麼?

callee是arguments對象的一個成員,它的值爲「正被執行的Function對象」。

什麼意思呢?

咱們寫個demo,看看輸出結果就知道啦。

代碼和結果圖見下

<!DOCTYPE html>
    <head>
        <title>callee</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
       <script>
           function obj(){
               //利用callee
               console.log(arguments.callee);
           }
           obj();
       </script>
    </body>
</html>

從上面的圖片可知,arguments.callee是指向參數arguments對象的函數,在這裏就是obj咯。

caller是什麼?

caller是函數對象的一個屬性,該屬性保存着調用當前函數的函數。

注意,是調用。不單單包含閉包哦。若是沒有父函數,則爲null。

仍是老樣子,咱們一直來寫個demo看看。

代碼以下:

<!DOCTYPE html>
    <head>
        <title>caller</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
       <script>
           //child是parent內的函數,並在parent內執行child
           function parent(){
               function child(){
                   //這裏child的父函數就是parent
                   console.log( child.caller );
               }
               child();
           }
           //parent1沒有被別人調用
           function parent1(){
               //這裏parent1沒有父函數
               console.log(parent1.caller);
           }
           //parent2調用了child2
           function parent2(){
               child2();
           }
           function child2(){
               console.log(child2.caller);
           }
           /*執行
             parent裏嵌套了child函數
             parent1沒有嵌套函數
             parent2調用了child2,child2不是嵌套在parent2裏的函數
           */
           parent();
           parent1();
           parent2();
       </script>
    </body>
</html>

打開chrome調試器,可得下效果圖

 

結合代碼和上圖理解,這下理解了caller了麼?

相關文章
相關標籤/搜索