搞清arguments,callee,caller
| 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了么?
搞清arguments,callee,caller的更多相关文章
- JavaScript中的內定物件與函式: arguments, callee, caller, this, apply(), call()
arguments, caller, callee, this都是用在函式(function)內的特殊內定物件.而apply()及call()則是用來呼叫函式的不同作法. arguments可用來取得 ...
- 关于arguments.callee.caller.arguments[0]获得event的一些问题
先从一个简单的例子说起,一个简单的button控件如下: < input type ='button' name ='mybtn' id ='mybtn' onclick ='myFun ...
- js arguments.callee & caller的用法及区别
在函数内部,arguments.callee该属性是一个指针,指向拥有这个arguments对象的函数; 而函数对象的另一个属性:caller,这个属性保存着调用当前函数的函数的引用,如果是在全局作用 ...
- javascript 中的 arguments,callee.caller,apply,call 区别
记录一下: 1.arguments是一个对象, 是函数的一个特性,只有在函数内才具有这个特性,在函数外部不用使用. 举例: function test(){ alert(typeof argume ...
- JavaScript中的arguments,callee,caller
在提到上述的概念之前,首先想说说javascript中函数的隐含参数: arguments: arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]argument ...
- 理解JavaScript中的arguments,callee,caller,apply
arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[n] 参数function :选项.当前正在执行的 Function 对象的名字. n : ...
- js的隐含参数(arguments,callee,caller)使用方法
在提到上述的概念之前,首先想说说javascript中函数的隐含参数: arguments arguments 该对象代表正在执行的函数和调用它的函数的参数.[function.]arguments[ ...
- JQuery Pagenation 知识点整理——arguments,callee,caller,apply应用(20150517)(转)
arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[n]参数function :选项.当前正在执行的 Function 对象的名字. n :选 ...
- arguments.callee.caller
1.Arguments Arguments是一个类似数组但不是数组的对象,说它类似数组是因为其具有数组一样的访问性质及方式,可以由arguments[n]来访问对应的单个参数的值,并拥有数组长度属性l ...
随机推荐
- Avoiding InvokeRequired
Just read a good article to do cross-thread calling in an easy and elegant way. It is amazing for it ...
- RDLC报表数据工具栏关闭后打开方法
显示方法为:Ctrl + Alt + D 快捷键 只做自己记录用
- Python 爬虫5——爬取并下载网页指定规格的图片
看完上篇文档之后,我们对于正则表达式已经有了基本的了解,其实学习最有效的办法就是带着问题和目的,这里我们假设有一个目标:获取某个网页上指定规格的图片的链接地址,并下载到本地. 一.实现步骤: 1.在浏 ...
- 【Beta】Daily Scrum Meeting第二次
1.任务进度 学号 已完成 接下去要做 502 系负责人及所负责专业的表 写出PHP测试的demo:将okHttp的请求放在非UI线程中执行 509 PHP更该用户信息:更新系负责人所负责系:删除任务 ...
- HTML5将图片转化成字符画
HTML5将图片转化成字符画 字符画大家一定非常熟悉了,那么如何把一张现有的图片转成字符画呢?HTML5让这个可能变成了现实,通过canvas,可以很轻松实现这个功能.其实原理很简单:扫描图片相应位置 ...
- VS2012中丢失ArcGIS模板的解决方法
VS2012中丢失ArcGIS模板的解决方法 由于ArcGIS10.0(for .NET)默认是用VS2010作为开发工具的,所以在先安装VS2012后装ArcGIS10.0 桌面版及ArcObjec ...
- SQL Server 2012安装图文教程
解析SQL Server 2012安装中心 当系统打开"SQL Server安装中心",则说明我们可以开始正常的安装SQL Server 2012了. SQL Server安装中心 ...
- ELK+Kafka集群日志分析系统
ELK+Kafka集群分析系统部署 因为是自己本地写好的word文档复制进来的.格式有些出入还望体谅.如有错误请回复.谢谢! 一. 系统介绍 2 二. 版本说明 3 三. 服务部署 3 1) JDK部 ...
- c#控制打印机杂项
因项目中需要用到控制打印机的相关信息,此贴将网络寻找的资料做了些整理 1. C# 如何设置系统的默认打印机 using System.Runtime.InteropServices; [DllIm ...
- TestNG 与 Junit的比较
转自 http://www.blogjava.net/fanscial/archive/2005/12/14/23780.html 1. JDK 5 Annotations (JDK ...