为你而来

javascript杂记1

1 var n=1;
2 
3 var f=new Function("return n;");
4 
5 new Function("x,y","return x+y;");

 

//用new Function定义的函数为顶级函数,能够获取顶级的变量,如n

 

 

1 ( function(a,b)
2   {
3    alert(typeof a+"\n"+typeof b);
4   }
5  )('a',1);

 

1 func=new Function("this.a='struts';");
2 
3 func();
4 alert(a);//显示struts

 

1 func=function(){
2         this.a='struts';
3     }
4 
5 
6 func();
7 alert(a);
8 
9 //也显示struts
 1     var func=new function(){this.a="java web"
 2     
 3     this.b=function(){return 'jsp';}
 4     
 5     
 6     
 7     };
 8 
 9     func();
10     alert(a);
11 
12 //不显示struts
13 //这里建立的是一个唯一的函数对象,它里面可以自己的方法和属性
14 //这跟new Function('x,y','return x+y')建立函数不同,
15 //其实new function()也是一个普通的对象,两者的区别好似string和String对象

 

 

 1 <script language='javascript'>
 2     var func=new function(){this.a="java web"
 3     
 4     this.b=function(){return 'jsp';}
 5     
 6     
 7     
 8     };
 9 
10     var myfunc=function(x){
11         var a="myfunc";
12         alert(this.b());
13         alert(x);
14     }
15     myfunc.call(func,"var");
16 </script>

 

posted on 2012-05-30 21:52  为你而来  阅读(211)  评论(0编辑  收藏  举报

导航