JavaScript 之arguments、caller 和 callee
来源:互联网 发布:yii2 开源 cms 编辑:程序博客网 时间:2024/05/16 23:56
1.前言
arguments, caller , callee 是什么?
在javascript 中有什么样的作用?本篇会对于此做一些基本介绍。
- arguments
arguments: 在函数调用时, 会自动在该函数内部生成一个名为 arguments的隐藏对象。 该对象类似于数组, 但又不是数组。可以使用[]操作符获取函数调用时传递的实参。
<!--by oscar999 2013-1-16--> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Arguments Test</title> </head> <body> <script> function testArg() { alert("real parameter count: "+arguments.length); for(var i = 0; i < arguments.length; i++) { alert(arguments[i]); } } testArg(11); //count: 1 testArg('hello','world'); // count: 2 </script> </body> </html>
看上去很简单。 需要注意的是 argument 保存的实参的信息。
上面有说, arguments 不是一个数组,何以见得? 执行以下部分就可以知道了
(function () { alert(arguments instanceof Array); // false alert(typeof(arguments)); // object })();
对于以上立即执行函数写法不清楚的话, 可以参考
http://blog.csdn.net/oscar999/article/details/8507919
只有函数被调用时,arguments对象才会创建,未调用时其值为null:
alert(new Function().arguments);//return null
arguments 的完整语法如下:
[function.]arguments[n]
参数function :选项。当前正在执行的 Function 对象的名字。 n :选项。要传递给 Function 对象的从0开始的参数值索引。
- caller
在一个函数调用另一个函数时,被调用函数会自动生成一个caller属性,指向调用它的函数对象。如果该函数当前未被调用,或并非被其他函数调用,则caller为null。
function testCaller() { var caller = testCaller.caller; alert(caller); } function aCaller() { testCaller(); } aCaller();
- callee
当函数被调用时,它的arguments.callee对象就会指向自身,也就是一个对自己的引用。
由于arguments在函数被调用时才有效,因此arguments.callee在函数未调用时是不存在的(即null.callee),且解引用它会产生异常。
function aCallee(arg) { alert(arguments.callee); } function aCaller(arg1, arg2) {aCallee();} aCaller();
转载自:http://blog.csdn.net/oscar999/article/details/8511335
0 0
- JavaScript 之arguments、caller 和 callee 介绍
- JavaScript 之arguments、caller 和 callee
- JavaScript 之arguments、caller 和 callee 介绍
- JavaScript 之arguments、caller 和 callee 介绍
- JavaScript 之arguments、caller 和 callee 介绍
- JavaScript 之arguments、caller 和 callee 介绍
- JavaScript -arguments、caller 和 callee
- javascript arguments(callee、caller)
- JavaScript arguments、callee、caller
- Javascript arguments,callee,caller
- JavaScript的arguments、caller和callee
- arguments.callee 和 caller
- arguments,callee和caller
- JavaScript中的arguments,callee,caller
- javascript中arguments,callee,caller
- JavaScript中的arguments、caller、callee
- js arguments和caller,callee
- javascript : arguments.callee.caller.arguments[0]
- 雷军豹变
- swift UI专项训练33 Switch开关
- eclipse的字体颜色修改
- OLAP的学习与实例搭建
- Html5 animation详解
- JavaScript 之arguments、caller 和 callee
- 在已经安装好spark的docker镜像里安装cassandra2.0.7
- JAVA,使用ant-1.6.5,压缩、解压缩辅助类代码
- 修复ios7 uitableviewcell 编辑状态下 删除按钮被覆盖
- 通知浏览器下载文件而不是打开文件
- BZOJ 3246 IOI 2013 Dreaming 树形DP
- wince 中用按钮替代SPIN CONTROL
- 黑马程序员_java_03_泛型
- HDU 1039 Easier Done Than Said