JavaScript 中的arguments,callee,caller,call,apply
				
									
					
					
						|  | 
							admin 2010年8月13日 23:3
								本文热度 5425 | 
					
				 
				
02 | 
<script language="JavaScript">  | 
 
04 | 
 * 演示arguments的用法,如何获取实参数和形数数  | 
 
06 | 
function argTest(a,b,c,d){  | 
 
07 | 
    var numargs = arguments.length;     // 获取被传递参数的数值。  | 
 
08 | 
    var expargs = argTest.length;       // 获取期望参数的数值。  | 
 
09 | 
    alert("实参数目为:"+numargs)  | 
 
10 | 
    alert("形数数目为:"+expargs)  | 
 
13 | 
    alert(argTest[0])          //undefined 没有这种用法  | 
 
19 | 
 *  arguments不是数组(Array类)  | 
 
22 | 
Array.prototype.selfvalue = 1;  | 
 
23 | 
function testAguments(){  | 
 
24 | 
    alert("arguments.selfvalue="+arguments.selfvalue);  | 
 
26 | 
//alert("Array.sefvalue="+new Array().selfvalue);  | 
 
31 | 
 * 说明:(当前函数).caller:返回一个对函数的引用,该函数调用了当前函数  | 
 
34 | 
function callerDemo() {  | 
 
35 | 
    if (callerDemo.caller) {  | 
 
36 | 
        var a= callerDemo.caller.arguments[0];  | 
 
39 | 
        alert("this is a top function");  | 
 
42 | 
function handleCaller() {  | 
 
47 | 
//handleCaller(" 参数1","参数2");  | 
 
51 | 
 * 说明:arguments.callee:初始值就是正被执行的 Function 对象,用于匿名函数  | 
 
53 | 
function calleeDemo() {  | 
 
54 | 
    alert(arguments.callee);  | 
 
57 | 
//(function(arg0,arg1) {alert("形数数目为:"+arguments.callee.length)})();  | 
 
61 | 
 * 说明:作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数方式有所区别:  | 
 
62 | 
 *       apply(thisArg,argArray);  | 
 
63 | 
 *     call(thisArg[,arg1,arg2…] ]);  | 
 
64 | 
 *     即所有函数内部的this指针都会被赋值为thisArg  | 
 
70 | 
    this.hit=function(msg){alert(msg)}  | 
 
71 | 
    this.info="我来自ObjectA"  | 
 
76 | 
    //调用ObjectA()方法,同时ObjectA构造函数中的所有this就会被 ObjectB中的this替代  | 
 
77 | 
    ObjectA.apply(this,arguments);//ObjectA.call(this);  | 
 
82 | 
 var value="global 变量";  | 
 
91 | 
 //Fun1.apply(new Obj());  | 
 
 
该文章在 2010/8/13 23:03:10 编辑过