caller,arguments.callee,call,apply
2009-04-18 00:39
344 查看
caller 返回调用当前函数的函数的引用
The caller property is available only within the body of a function. If used outside a function declaration, the caller property is null.
If the currently executing function was invoked by the top level of a JavaScript program, the value of caller is null.
The this keyword does not refer to the currently executing function, so you must refer to functions and Function objects by name, even within the function body.
The caller property is a reference to the calling function, so
If you use it in a string context, you get the result of calling functionName.toString. That is, the decompiled canonical source form of the function.
You can also call the calling function, if you know what arguments it might want. Thus, a called function can call its caller without knowing the name of the particular caller, provided it knows that all of its callers have the same form and fit, and that they will not call the called function again unconditionally (which would result in infinite recursion).
PS: Opera9.5- can't support Caller,return 'undefiend'
arguments.callee
返回当前运行的Function函数体,说起很别扭,因为alert(arguments.callee) 输出的确实是一段当前函数体(其实可以理解为就是调用函数本身)
a property whose value is the function reference.
arguments.callee refers to the function that is currently running. It provides a way for an unnamed function to refer to itself. This property is defined only within a function body.
arguments.length :实际参数长度
arguments.callee.length :形参长度
DemoCallee === arguments.callee :这两者是相等的
call,apply
ECMAScript specifies two methods that are defined for all functions, call() and apply(). These methods allow you to invoke a function as if it were a method of some other object. The first argument to both call() and apply() is the object on which the function is to be invoked; this argument becomes the value of the this keyword within the body of the function. Any remaining arguments to call() are the values that are passed to the function that is invoked.
The caller property is available only within the body of a function. If used outside a function declaration, the caller property is null.
If the currently executing function was invoked by the top level of a JavaScript program, the value of caller is null.
The this keyword does not refer to the currently executing function, so you must refer to functions and Function objects by name, even within the function body.
The caller property is a reference to the calling function, so
If you use it in a string context, you get the result of calling functionName.toString. That is, the decompiled canonical source form of the function.
You can also call the calling function, if you know what arguments it might want. Thus, a called function can call its caller without knowing the name of the particular caller, provided it knows that all of its callers have the same form and fit, and that they will not call the called function again unconditionally (which would result in infinite recursion).
function DemoCaller() { if (DemoCaller.caller) { alert(DemoCaller.caller.toString()); } else { alert("this is a top function"); } } function CallerTest() { DemoCaller(); }
<button onclick="DemoCaller()"> Button 1 </button> <button onclick="CallerTest()"> Button 2 </button>
PS: Opera9.5- can't support Caller,return 'undefiend'
arguments.callee
返回当前运行的Function函数体,说起很别扭,因为alert(arguments.callee) 输出的确实是一段当前函数体(其实可以理解为就是调用函数本身)
a property whose value is the function reference.
arguments.callee refers to the function that is currently running. It provides a way for an unnamed function to refer to itself. This property is defined only within a function body.
function(x) { if (x <= 1) return 1; return x * arguments.callee(x-1); }
(function DemoCallee(a,b,c){ alert("实参长度 arguments.length = " + arguments.length) // 2 alert("形参长度 arguments.callee.length = " + arguments.callee.length) // 3 alert("形参长度 DemoCallee.length = " + DemoCallee.length) // 3 alert("DemoCallee === arguments.callee : " + (DemoCallee == arguments.callee)) // true })(1,2)
arguments.length :实际参数长度
arguments.callee.length :形参长度
DemoCallee === arguments.callee :这两者是相等的
call,apply
function ClassA(sColor) { this.color = sColor; this.sayColor = function () { alert(this.color); }; } function ClassB(sColor, sName) { ClassA.call(this, sColor); this.name = sName; this.sayName = function () { alert(this.name); }; } var objA = new ClassA("red"); var objB = new ClassB("blue", "Nicholas"); objA.sayColor(); // red objB.sayColor(); // blue objB.sayName(); // Nicholas
ECMAScript specifies two methods that are defined for all functions, call() and apply(). These methods allow you to invoke a function as if it were a method of some other object. The first argument to both call() and apply() is the object on which the function is to be invoked; this argument becomes the value of the this keyword within the body of the function. Any remaining arguments to call() are the values that are passed to the function that is invoked.
相关文章推荐
- ECMAScript 中的call、apply、arguments、caller和callee的学习
- arguments、caller、callee、apply、call对象
- 理解JavaScript中的arguments,callee,caller,apply 和call
- arguments,caller,callee,apply,call
- js中的arguments,apply,call,callee,caller
- javascript 的函数调用过程中的arguments,callee,caller,apply,call
- javascript 中的 arguments,callee.caller,apply,call 区别
- apply call this arguments caller callee
- 【转】全面理解javascript的arguments,callee,caller,call,apply概念(修改版)
- javascript中call,apply,arguments,callee,caller的用法
- arguments.callee 调用自身 caller,callee,apply and call
- 函数的arguments,caller,callee,call,apply
- javascript 中的 arguments,callee.caller,apply,call 区别
- 全面理解javascript的Arguments,caller,callee,call,apply
- [zz]javascript的arguments,caller,callee,call,apply样例及理解
- javascript下arguments,caller,callee,call,apply示例及理解
- [Javascript Function] Arguments, call(), apply(), caller(), callee()
- javascript的caller,callee,call,apply以及arguments
- javascript下arguments,caller,callee,call,apply示例及理解
- javascript下的arguments,caller,callee,call,apply示例及理解