arguments並不屬於很偏的一個知識點,但我以前一直覺得它是一個以實參爲元素的數組。實際上arguments
並非一個數組,而是一個只具備length
屬性的類數組,數組的其它方法它都不具有。能夠經過.call()
執行silce
方法它轉換爲數組。 argument
有一個重要的參數callee
指向當前執行的函數自己,也就是說們經過它能夠遞歸執行函數。 例如,使用setTimeout
來實現setInterval,能夠這樣寫:數組
setTimeout(function(){
//do something
setTimeout(arguments.callee,200);
},200);
複製代碼
同時,callee
也能夠接收參數:bash
function add(){
let num = arguments[0]
num++
console.log(num)
if(num<10){
arguments.callee(num)
}
}
add(0)//1,2,3,4,...,10
複製代碼
而caller
則是指向當前運行函數的調用者:函數
function handleAdd(){
let num = arguments[0]
add = function (){
num ++
console.log(num)
console.log(arguments.callee.caller)//function handlerAdd
console.log(arguments.callee.caller==add.caller)//true
}
add()
}
handleAdd(0)
複製代碼