【function定义】

 A.

function alertArgsCount()
{
   alert("函数调用时的参数个数:"+arguments.length);
}

 

B.

var funBody="if(a>b) return a-b; else return b-a;"
var  getDiffValue=new Function("a","b",funBody);

alert(getDiffValue(12,125));
alert("函数定义时的参数个数为:"+getDiffValue.length);

 

【函数调用】

setTimeout(function add1(x,y){alert(x+y)},2000,3,5)会在2秒后弹出8.(但在ie下会出错)

 var func=new function(){this.a="func"}
    var myfunc=function(x){
        var a="myfunc";
        alert(this.a);
        alert(x);
    }
    myfunc.call(func,"var");//分别弹出了func和var

func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(func1,[var1,var2,var3])

 

【arguments调用】

function Base()
{
   if(arguments.length==1)
     {
      if(arguments[0]>0)
          {
            alert("大于0");          

          }
       else
            alert("不大于0");
      }
    else if(arguments.length==2)
      {           
          var r=(arguments[0]>arguments[1]);
          switch(r)
          {
              case true: alert("true"); break;
              case false: alert("false");break;
              default: alert("do not know!");
          }

      }     
}     

    Base(10);
    Base(10,20);   

 【匿名函数】

1 (function(obj){
2   // ToDo about obj
3 })(testobject);

 

posted on 2010-11-14 17:44  极简  阅读(375)  评论(0编辑  收藏  举报