arguments 是什麼?
arguments 是函數調用時創建的一個類似陣列但又不是陣列的物件,並且它存儲的是實際傳遞給函數的參數,並不局限於函數聲明的參數列表哦。
什麼意思?
寫個 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 也在內。
接下來,我們修改上面的程式碼,在調用 obj 函數時給它傳遞參數,但 obj 函數是沒有參數的。
具體程式碼見下:
<!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。
所以說,為什麼 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 了麼?