arguments、callee、caller是什麼?

//arugumets是個神奇的語法,就算定義的function括號裡面不設定參數,arguments還是會儲存呼叫自己函式時裡面所帶的參數,是以類似陣列的物件儲存,此語法只能在function內使用。
function child() {
  console.log("(arguments)呼叫child()函式時所傳的參數: %o", arguments);
  console.log(
    "(arguments.callee)arguments參數本身的函式: %O",
    arguments.callee
  );
  console.log(
    "(arguments.callee.caller)呼叫arguments本身函式child()的函式: %O",
    arguments.callee.caller
  );
  console.log("(child.caller同上)呼叫child()的函式: %O", child.caller);
  var childSay = `小孩說: 媽~好的,我會乖乖去${[...arguments].join("、")}。`;
  console.log(childSay);
}

function mother() {
  var motherSay = `媽媽說: 孩子,媽媽希望你能健康快樂,所以要健康快樂的活著就要定時地: ${[
    ...arguments,
  ].join("、")}。`;
  console.log(motherSay);
  child(...arguments);
}

mother("吃飯", "洗澡", "睡覺");

/*result:
媽媽說: 孩子,媽媽希望你能健康快樂,所以要健康快樂的活著就要定時地: 吃飯、洗澡、睡覺。
(arguments)呼叫child()函式時所傳的參數: Arguments(3) ["吃飯", "洗澡", "睡覺", callee: ƒ, Symbol(Symbol.iterator): ƒ]
(arguments.callee)arguments參數本身的函式: ƒ child()
(arguments.callee.caller)呼叫arguments本身函式child()的函式: ƒ mother()
(child.caller同上)呼叫child()的函式: ƒ mother()
小孩說: 媽~好的,我會乖乖去吃飯、洗澡、睡覺。 */

PS. console.log 裡變數要以何種方式呈現,可使用字串替換符。

profile-image
Hi, 我是 Zeki。目前為一名前端工程師。我相信科技始終來自於人性,是用來幫助人們過上更有品質的生活的,但願也希望如此。