javascript知识持续更新中...
javascript函数入门到高级:http://blog.csdn.net/a125138/article/details/7824544

解决方案 »

  1.   

    更多javascript资料请访问:http://blog.csdn.net/a125138
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
    <HTML>  
     <HEAD>  
      <TITLE> New Document </TITLE>  
      <META NAME="Generator" CONTENT="EditPlus">  
      <META NAME="Author" CONTENT="">  
      <META NAME="Keywords" CONTENT="">  
      <META NAME="Description" CONTENT="">  
      <script type="text/javascript">  
        //裴波那数字  
        function text(num){  
            if(num==0){  
                return 0;  
            }  
            if(num==1){  
                return 1;  
            }  
            return arguments.callee(num-1)+arguments.callee(num-2); //递归调用  
        }  
        /*  
        定义函数的四种方式:关键字:function 
        方法一: 
                function 函数名([参数列表]){ 
                     
                } 
        方法二: 
                var 函数名=function([参数列表]){ 
                     
                } 
        方法三: 
                var 函数名=function 函数名([参数列表]){ 
                     
                } 
        方法四:(很少用到,可读性差) 
                var 函数名=new Function([参数列表],body); 
     
        没有返回的函数,返回值是undefined; 
        函数,有一个属性length,表达定义函数时的参数的个数 
        函数内部,有一个属性arguments,表示执行函数时,真正传递的函数的类数组的对象 
        arguments有length属性,表示运行时传递的参数的个数 
        arguments有callee属性,它指向对象本身 
     
        函数,有一个属性prototype,表示函数的原型,它是一个对象。 
        当我们将函数做构造器,创建一个对象时,解析器会将prototype属性所指向原型对象中的所有的属性和方法都拷贝到新创建的对象中。 
        如果保存对象状态或私有属性或方法,应该放在函数内部。 
        如果是通用的,公共的方法,应该放在函数的原型中。 
     
        this,与其它面向对象语言不同是,javascript中的this指针不一定指向当前对象。 
        call(),apply()方法: 
        指定this指向。指定函数执行上下文对象。 
            call(上下文对象,[参数1,参数2,。]) 
            apply(上下文对象,[参数数组]) 
        */  
      
        //不常用的函数声明方法,  
        var test = new Function("a","b","alert('sum:'+(a+b));");  
        var test = new Function("a,b","alert('sum:'+(a+b));"); //效果同上  
        test(1,2);  
      
        function fun(){};  
      
        //函数作为对象来使用  
          
        //为函数对象fun添加属性  
        fun.a = "a";  
      
        //为函数对象fun添加方法  
        fun.b = function(){alert("b()");}  
      
        alert(fun.a);  
        fun.b();  
          
        //函数作为构造器来使用  
        function Person(){  
            this.name = "张三";//添加属性  
      
            //添加方法  
            this.show = function(){  
                alert("name:"+this.name);  
            }     
        }  
      
        var p = new Person();  
        alert(p instanceof Person);  
        alert(p.name);  
        p.show();  
          
      
        //模拟访问权限  
        function Person(){  
            this.name = "张三";//public  
            var age = 22;       //private  
            //添加方法  
            this.show = function(){  
                alert("name:"+this.name);  
            }     
      
            //提供public方法来访问private  
            this.getAge = function(){  
                return age;  
            }  
        }  
      
      
        //匿名函数的调用  
        (function(name,age,gender){  
            this.name=name;  
            this.age=age;  
            this.gender=gender;  
            alert(this.name+","+this.age+","+this.gender);  
        })("续写经典","18","男");  
          
        //函数的调用  
        (function sum(a,b,c,d){  
            alert(a+b+c+d);  
            alert(sum.length); //定义函数的参数的个数  
            alert(arguments.length); //实际传递参数的个数  
        })(1,2,4,5,6);  
          
      
        function Person(name,age){  
            this.name = name;  
            this.age = age;  
        }  
        //为Person的原型对象添加属性  
        Person.prototype.sex = "男";  
        //为Person的原型对象添加方法  
        Person.prototype.show = function(){  
            alert(this.name+","+this.age+","+this.sex);  
        }  
        //查看原型中的属性和方法  
        var pt = Person.prototype;  
        for(var pro in pt){  
            alert(pro);  
        }  
      
        //为object的原型添加公用的(取最大值)的方法  
        Object.prototype.getMax = function(arr){  
            if(!arr instanceof Array)  
            {  
                alert("必须是数组");  
            }  
            else{  
                for(var i=1,max = arr[0];i<arr.length;i++)  
                {  
                    if(max < arr[i]){  
                        max = arr[i];  
                    }  
                }  
                return max;  
            }  
        }  
        var arr = [54,1,654,14,21];  
        var o = new String();  
        alert(o.getMax(arr));  
      
      
        //创建两个对象  
        var p1 = {name:"p1"};   //new Object();简写  
        var p2 = {name:"p2"};  
      
        function show(msg){  
            if(msg){  
                alert(msg+","+this.name);   //默认this == window  
            }  
            else{  
                alert(name);  
            }  
        }  
      
        show.call(p1,["this指向p1"]);  
        show.call(p2,["this指向p2"]);  
        show.call(window);  
          
        //this指向对象的简单分析  
        function p1(){  
            this.name = "我是对象p1";  
            this.show = function(){  
                return this.name;   //this指向的是本身的name属性  
            };  
        }  
        function p(){  
            this.show = function(){  
                return this.name;   //this指向的是window对象的name属性  
            };  
        }  
        var a = new p();  
        document.write(a.show()+"<br />");  
        var b = new p1();  
        document.write(b.show()+"<br />");  
      
      </script>  
     </HEAD>  
      
     <BODY>  
        
     </BODY>  
    </HTML> 
      

  2.   


    /*
    JavaScript反射机制:
    反射机制是指程序在运行期间能够获取自身的信息,例如一个对象能够在运行时知道自己拥有哪
    些方法和属性,并且可以调用这些方法和属性。在C#和Java中都提供了反射机制,能够在运行时动
    态判断和调用自己的属性或方法。在JavaScript中可用for(…in…)语句来实现反射
    */

    function stu(){
    this.one="续写经典";
    this.two="新的一天,qy会更好更专业!";
    }
    stu.prototype.showStu=function(){
    alert("我是:"+this.one+"\n签名:"+this.two);
    }
    var student=new stu();
    student.showStu();
    //反射的简单应用
    for(var s in student){
    if(typeof(student[s])=="function"){
    student[s]();
    }else{
    alert(student[s]);
    }
    }
      

  3.   

    更多javascript资料请访问:http://blog.csdn.net/a125138利用共享prototype实现继承
    类之间的继承关系是现实世界中遗传关系的直接模拟,它表示类之间的内在联系以及对属性和操
    作的共享,即子类可以沿用父类(被继承的类)的某些特征。当然子类也可以具有自己独立的属性和操
    作。
    继承也是软件复用的一种形式,新类由已存在的类生成,通过保留它们的属性和行为并且根据新
    类的要求对性能加以修改,添加新的属性和行为。虽然在JavaScript中没有专门的机制来实现类的
    继承,但可以通过复制一个类的prototype到另外一个类来实现继承。下面是一种简单的继承实现,
    代码如下:
    <script language="javascript">
    function HelloClass(){
    //构造方法
    }
    function HelloSubClass(){
    //构造方法
    }
    HelloSubClass.prototype=HelloClass.prototype;
    HelloSubClass.prototype.Propertys="name";
    HelloSubClass.prototype.subMethods=function(){
    //方法实现代码
    alert("in Methods");
    }
    var obj=new HelloSubClass();
    obj.subMethods();
    </script>
    在以上的代码中,首先是HelloSubClass具有了和HelloClass一样的prototype,如果不考
    虑构造方法,则两个类是等价的。随后,又通过prototype给HelloSubClass赋予了额外的属性和方法
    所以HelloSubClass是在HelloClass的基础上增加了新的属性和方法,从而实现了类的继承。