javascript中arguments,callee,caller詳解
arguments是什么?
arguments是函數(shù)調(diào)用時(shí),創(chuàng)建的一個(gè)類(lèi)似的數(shù)組但又不是數(shù)組的對(duì)象,并且它存儲(chǔ)的是實(shí)際傳遞給函數(shù)的參數(shù),并不局限于函數(shù)聲明的參數(shù)列表哦。
尼瑪,什么意思?
寫(xiě)個(gè)demo看看,代碼見(jiàn)下
<!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>
運(yùn)行該代碼,通過(guò)chrome調(diào)試器,可得下圖

我利用instanceof判斷arguments,從打印的效果看,arguments是一個(gè)對(duì)象。
然后展開(kāi)打印出的arguments,可以從上圖得知,它里面包括了許多屬性,callee也在內(nèi)。
接下來(lái),我們修改上面的代碼,在調(diào)用obj函數(shù)時(shí),給它傳遞參數(shù),但obj函數(shù)是沒(méi)有參數(shù)的。
具體代碼見(jiàn)下
<!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傳遞參數(shù)
obj('monkey','love',24);
</script>
</body>
</html>
通過(guò)chrome調(diào)試器,可得下圖

大家可以看見(jiàn),arguments包含了三個(gè)我們給它傳遞的參數(shù)”monkey”,”love”,24。
所以說(shuō),為什么arguments是存儲(chǔ)的實(shí)際傳遞給函數(shù)的參數(shù)呢,而不是函數(shù)聲明的參數(shù)。
callee是什么?
callee是arguments對(duì)象的一個(gè)成員,它的值為“正被執(zhí)行的Function對(duì)象”。
什么意思呢?
我們寫(xiě)個(gè)demo,看看輸出結(jié)果就知道啦。
代碼和結(jié)果圖見(jiàn)下
<!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是指向參數(shù)arguments對(duì)象的函數(shù),在這里就是obj咯。

caller是什么?
caller是函數(shù)對(duì)象的一個(gè)屬性,該屬性保存著調(diào)用當(dāng)前函數(shù)的函數(shù)。
注意,是調(diào)用。不僅僅包含閉包哦。如果沒(méi)有父函數(shù),則為null。
還是老樣子,我們一直來(lái)寫(xiě)個(gè)demo看看。
代碼如下:
<!DOCTYPE html>
<head>
<title>caller</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<script>
//child是parent內(nèi)的函數(shù),并在parent內(nèi)執(zhí)行child
function parent(){
function child(){
//這里child的父函數(shù)就是parent
console.log( child.caller );
}
child();
}
//parent1沒(méi)有被別人調(diào)用
function parent1(){
//這里parent1沒(méi)有父函數(shù)
console.log(parent1.caller);
}
//parent2調(diào)用了child2
function parent2(){
child2();
}
function child2(){
console.log(child2.caller);
}
/*執(zhí)行
parent里嵌套了child函數(shù)
parent1沒(méi)有嵌套函數(shù)
parent2調(diào)用了child2,child2不是嵌套在parent2里的函數(shù)
*/
parent();
parent1();
parent2();
</script>
</body>
</html>
打開(kāi)chrome調(diào)試器,可得下效果圖

結(jié)合代碼和上圖理解,這下理解了caller了么?
- Javascript函數(shù)中的arguments.callee用法實(shí)例分析
- Javascript中arguments和arguments.callee的區(qū)別淺析
- js的隱含參數(shù)(arguments,callee,caller)使用方法
- js中arguments,caller,callee,apply的用法小結(jié)
- js arguments,jcallee caller用法總結(jié)
- javascript下arguments,caller,callee,call,apply示例及理解
- js arguments.callee的應(yīng)用代碼
- JavaScript arguments.callee作用及替換方案詳解
相關(guān)文章
JavaScript中的document.referrer在各種瀏覽器測(cè)試結(jié)果
這篇文章主要介紹了JavaScript中的document.referrer在各種瀏覽器測(cè)試結(jié)果,包括在多種情況下每個(gè)瀏覽器能否用document.referrer取到值,非常珍貴的測(cè)試結(jié)果,需要的朋友可以參考下2014-07-07
簡(jiǎn)介JavaScript中g(shù)etUTCMonth()方法的使用
這篇文章主要介紹了簡(jiǎn)介JavaScript中g(shù)etUTCMonth()方法的使用,是JS入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06
JavaScript DOM學(xué)習(xí)第六章 表單實(shí)例
在這一章我有一個(gè)檢查用戶輸入然后打印用戶輸入的表單和代碼。下面我會(huì)著重講解檢測(cè)文本的代碼。2010-02-02
javascript instanceof 與typeof使用說(shuō)明
instanceof和typeof都能用來(lái)判斷一個(gè)變量是否為空或是什么類(lèi)型的變量。2010-01-01
javascript學(xué)習(xí)筆記(五) Array 數(shù)組類(lèi)型介紹
javascript學(xué)習(xí)筆記之Array 數(shù)組類(lèi)型介紹,需要的朋友可以參考下2012-06-06
推薦一些非常不錯(cuò)的javascript學(xué)習(xí)資源站點(diǎn)
推薦一些非常不錯(cuò)的javascript學(xué)習(xí)資源站點(diǎn)...2007-08-08
基于js 字符串indexof與search方法的區(qū)別(詳解)
下面小編就為大家分享一篇基于js 字符串indexof與search方法的區(qū)別介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
JavaScript常用字符串與數(shù)組擴(kuò)展函數(shù)小結(jié)
這篇文章主要介紹了一些JavaScript常用字符串與數(shù)組擴(kuò)展函數(shù),整理了一些簡(jiǎn)單而使用率又高的操作String與Array的函數(shù),需要的朋友可以參考下2016-04-04

