Function: caller,arguments.callee,call,apply,

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).

 

 

Caller JavaScript Code
 1 function DemoCaller() {
 2     if (DemoCaller.caller) {
 3         alert(DemoCaller.caller.toString());
 4     } else {
 5         alert("this is a top function");
 6     }
 7 }
 8 function CallerTest() {
 9     DemoCaller();
10 }

 

 

 

Caller Html Code
1<button onclick="DemoCaller()"> Button 1 </button>
2<button onclick="CallerTest()"> Button 2 </button>

 


PS: Opera 不支持Caller,返回 undefiend ,( 是不是很BT? ) {更新下,新的Opera9.5+已经支持}

 


 

 

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);
}

Callee JS Code
1 function DemoCallee(a,b,c){
2     alert("实参长度 arguments.length = " + arguments.length) // 2
3     alert("形参长度 arguments.callee.length = " + arguments.callee.length) // 3
4     alert("形参长度 DemoCallee.length = " + DemoCallee.length) // 3 
5     alert("DemoCallee === arguments.callee : " + (DemoCallee == arguments.callee)) // true
6 }
7 DemoCallee(1,2)


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.

实际上就是改变当前函数的函数指针,类似对象冒充。这两个方法的不同只是在第个参数上

Call JS Code
 1 function ClassA(sColor) {
 2     this.color = sColor;
 3     this.sayColor = function () {
 4         alert(this.color);
 5     };
 6 }
 7 
 8 function ClassB(sColor, sName) {
 9     ClassA.call(this, sColor);
10     this.name = sName;
11     this.sayName = function () {
12         alert(this.name);
13     };
14 }
15 
16 var objA = new ClassA("red");
17 var objB = new ClassB("blue""Nicholas");
18 
19 objA.sayColor(); // red
20 objB.sayColor(); // blue 
21 objB.sayName();  // Nicholas
22 

转载于:https://www.cnblogs.com/qieqing/archive/2008/06/17/1224060.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值