javascript函数

一、函数的声明方式

 1 //声明方式
 2 function box(num1,num2){        //普通函数的声明方式
 3     return num1+num2;
 4 }
 5 alert(box(1,2));
 6 var box=function(num1,num2){    //使用变量的方式
 7     return num1+num2;
 8 };
 9 alert(box(1,2));
10 
11 var box=new Function('num1','num2','return num1+num2');        //使用new的构造方法来声明函数
12 alert(box(1,2));                                                                                //这种方式不推荐
13 alert(typeof box);        

二、函数可以传递函数(函数也是对象,函数名是指向函数的指针)

 1 //1.(不是作为函数来传递的,而是作为函数的返回值来传递的)。
 2 function box(sum,num){
 3     return sum+num;
 4 }
 5 function sum(num){
 6     return num+10;
 7 }
 8 var result=box(sum(10),10);        //box(20,10),sum(10)这里传递的是函数的返回值,和普通的变量一样,没区别
 9 alert(result);
10 
11 //2.作为值的函数。即要把函数本身作为参数传递,而不是函数的结果
12 function box(sum,num){
13     return sum(num);
14 }
15 function sum(num){
16     return num+10;
17 }
18 var result=box(sum,10);        //这里sum是一个函数,当作参数传递到另外一个函数里,而不是函数的返回值
19 alert(result);

三、函数内部对象

 

  1 //函数内部对象
  2 function box(num){
  3     if(num<=1){
  4         return 1;
  5     }else{
  6         return num*box(num-1);            //4*3*2*1=24,阶乘,递归
  7     }
  8 }
  9 alert(box(4));
 10 
 11 //函数属性,arguments.callee,就是调用自己本身的函数
 12 function sum(num){
 13     if(num<=1){
 14         return 1;
 15     }else{
 16         return num*arguments.callee(num-1);            //使用arguments.callee,调用自身,实现递归
 17     }
 18 }
 19 alert(sum(4));
 20 
 21 //this,在函数内部就代表函数作用域;如果在全局,就是window作用域
 22 //window是一个对象,而且是JS里面最大的对象,是最外围的对象
 23 //alert(window);    //[object Window]
 24 //alert(typeof window);    //object,window是对象,类型是对象
 25 alert(this);    //[object Window],this目前表示的是window,因为在window的范围下
 26 alert(typeof this);    //object,和window一摸一样,所以this就是window
 27 
 28 var color='red';    //这里color就是全局变量,而这个变量又是window的属性;
 29 //alert(window.color);    //这里已经很好的说明color是window下的属性
 30 alert(this.color);    //同上
 31 
 32 window.color='red';    //    相当于 var color='red';
 33 alert(this.color);
 34 
 35 window.color='red';    //    相当于 var color='red';
 36 var box={
 37     color:'blue',        //这里的color是box下的属性,也就是局部变量
 38     sayColor:function(){
 39         alert(this.color);    //这里的this,代表box的对象
 40     }
 41 };
 42 alert(this.color);    //这里this代表window
 43 box.sayColor();
 44 
 45 window.color='red';
 46 function sayColor(){
 47     alert(this.color);
 48 }
 49 sayColor();        //这里的sayColor,其实范围还是在window下
 50 
 51 window.color='red';
 52 function sayColor(){        //所以这里执行的时候是动态的,第一次是在window下,第二次是在box下
 53     alert(this.color);
 54 }
 55 sayColor();        //这里的sayColor,其实范围还是在window下
 56 var box={
 57     color:'blue'    
 58 }
 59 box.sayColor=sayColor;    //这段代码相当于48行
 60 box.sayColor();    //这里执行的是box里面的this.color
 61 
 62 //函数是对象,本身也有属性和方法
 63 function box(name,age){
 64     alert(name+age);
 65 }
 66 alert(box.length);
 67 
 68 //apply()和call()方法,第一个参数,用来指定作用域,this表示window作用域;
 69 //apply()第二个参数,传参数数组
 70 //call()第二个参数,必须传递参数是一个一个的传
 71 
 72 function box(num1,num2){
 73     return num1+num2;
 74 }
 75 function sum(num1,num2){        //apply和call可冒充另外一个函数
 76     return box.apply(this,[num1,num2]);    //this,表示window作用域 ,[]表示传递的参数
 77 }
 78 function sum2(num1,num2){
 79     return box.apply(this,arguments);    
 80 }
 81 //alert(box(10,20));
 82 //alert(sum(10,10));
 83 alert(sum2(10,10));
 84 
 85 function box(num1,num2){
 86     return num1+num2;
 87 }
 88 function sum(num1,num2){
 89     return box.call(this,num1,num2);        //call只是传递参数不同,其他和apply一样
 90 }
 91 alert(sum(10,10));
 92 
 93 //使用call()和apply()来改变作用域
 94 var color='red';    //全局
 95 var box={
 96     color:'blue'        //局部
 97 }
 98 function sayColor(){
 99     alert(this.color)
100 }
101 //sayColor();        //全局
102 //用call来实现对象冒充,冒充box下,冒充window下
103 //sayColor.call(window);        //冒充window,red
104 //sayColor.call(this);        //this就是window
105 sayColor.call(box);        //冒充box,作用域就在box里面,所以color就是blue

 

posted @ 2012-04-29 13:43  yxmichael  阅读(169)  评论(0编辑  收藏  举报