JavaScript中的arguments,callee,caller,call,appy

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript中的arguments,callee,caller,call,appy</title>
<script type="text/javascript">
//----------------arguments基本使用------------------------// 
function argTest(a,b,c,d){
    var numargs = arguments.length;     // 实参个数
    var expargs = argTest.length;       // 形参个数。
    alert("实参数目为:"+numargs);
    alert("形数数目为:"+expargs);

    alert("arguments[0]="+arguments[0]);      //这个在IE和火狐下都支持 
    alert("argTest[0]="+argTest[0]);          //IE8.0-undefined,Mozilla/5.0 支持。
    alert(typeof(arguments));                 //arguments是对象
}
argTest(1,2) 
//一句话,arguments.length就是实参个数,函数名.length就是形参个数//
//----------------caller基本使用------------------------//  
function callerDemo() {
    if (callerDemo.caller) {
       alert(typeof(callerDemo.caller));  //返回定义式类型(function)
       alert(callerDemo.caller);          //返回上级函数的引用
        var a= callerDemo.caller.arguments[0];
        alert(a);
    } else {
        alert("this is a top function");
        alert(callerDemo.caller);
        alert(typeof(callerDemo.caller));   //typeof(null)返回object
    }
}
function handleCaller() {
    callerDemo();
}

callerDemo();
handleCaller("参数1","参数2");

function calleeDemo() {
    alert(arguments.callee); //正被执行的 function 对象引用
}
//----------------这到底怎么了???-----------------------//
   function Sing()
    {
    with(arguments.callee)
       alert(arguments.callee);
       alert(author + ":" + poem);
    };
    Sing.author = "李白";
    Sing.poem = "汉家秦地月,流影照明妃。一上玉关道,天涯去不归";
    Sing();
    Sing.author = "李战";
    Sing.poem = "日出汉家天,月落阴山前。女儿琵琶怨,已唱三千年";
    Sing();
//这个只输出一个弹出框,应该共有六个呀?
//原因如下:alert(arguments.callee)执行后当前引用(arguments.callee)变成了alert函数引用,这个引用中没有author和poem属性。
//----------------还是谨慎调用arguments.callee-----------------------//

//----------------caller与匿名定义式基本使用------------------------//
//匿名定义式调用;
(function(arg0,arg1){
 alert(arguments.callee);
 alert("参数个数为:"+arguments.callee.length);//这里到底是形参个数还是实参个数?看如下分析。
})();

function anonymousFnCall()
{
  alert("Hello world");
  this.a="第一个属性";
  //一个匿名定义式
  this.show=function (arg0,arg1)
  {
        alert(this.a);
        alert("参数个数为:"+arguments.callee.length)
  };
   this.sayHello=function()
  {
      return function()   {alert("hello"); }   ;
  };
}

var ano=new anonymousFnCall();
ano.show('1'); //已经表明arguments.callee.length是形参个数了,因为arguments.callee相当于 函数名.length
ano.sayHello()();//对定义式的调用不要忘记();
//一句话,函数名.callee是对上一级函数的引用,arguments.callee是对正在运行的函数引用//
//-----------------apply与call---------------------//
function ObjectA(){
    this.getMsg=function(msg){alert(msg)}
 }
 
 function ObjectB(){
    //ObjectA.call(this);
    ObjectA.apply(this,arguments);
    getMsg('Object参数');
 }
ObjectB('参数0');
//-----------------apply与call调用差别---------------------//
function act(){
 for(var i=0;i<arguments.length;i++){
  document.write(this.t[i]+": "+arguments[i]+"<br/>");
 }
}
function actby(){
 this.t = ["参数1是:","参数2是:"];
}
//call(obj,arg1,arg2...);
act.call(new actby(),"参数1调用来自call","参数2调用来自call");
//apply(obj,[arg1,arg2...]);
act.apply(new actby(),["参数1调用来自apply","参数2调用来自apply"]);
//--apply与call都是将属性或是定义式绑定到另外一个对象上去运行--//
</script>
</head>
<body>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值