guanxf

          我的博客:http://blog.sina.com.cn/17learning

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            71 隨筆 :: 1 文章 :: 41 評論 :: 0 Trackbacks
          javaScript沒有集成的對象,所以采用下面三種方法模擬:

          1、js原型實現繼承
          function Person(name,age){
            this.name=name;
            this.age=age;
          }
          Person.prototype.sayHello=function(){
             document.write("使用原型得到Name:"+this.name+"</br>");
          }

           //var per=new Person("zhangping","21");
            //per.sayHello();


           function Student(){}
           Student.prototype=new Person("zhangping","21");
           var stu=new Student();
           Student.prototype.gade="3";
            Student.prototype.intr=function(){
               document.write(this.gade);
           }
           stu.sayHello();
          stu.intr();
          */
          2、構造函數實現繼承
          /*
          function  Parent(name){
             this.name=name;
             this.sayParent=function(){
               document.write("Parent:"+this.name);
             }
          }
          function  Child(name,age){
            this.tempMethod=Parent;
            this.tempMethod(name);
            /*
            this.age=age;
            this.sayParent=function(){
               document.write("Child:"+this.name+"age:"+this.age);
             }
             */
             /*
          }
          var parent=new Parent("zhangping");
          parent.sayParent();
          var child=new Child("xiaoguanxianfei","11");
          child.sayParent();
          */

          3、使用Call Applay實現繼承

          function  Person(name,age,love){
              this.name=name;
              this.age=age;
              this.love=love;
              this.say=function say(){
                 document.write("姓名:"+name);
              }
          }
          function student(name,age){
             Person.call(this,name,age);
          }
          function teacher(name,love){
             Person.apply(this,[name,love]);
          }
          var per=new Person("zhangping","21","guanxianfei");
          per.say();
          var stu=new student("guanxianfei","22");
          stu.say();
          var tea=new teacher("xiaoguanxianfei","22");
          tea.say();
          posted on 2011-12-15 00:55 管先飛 閱讀(2182) 評論(3)  編輯  收藏

          評論

          # re: js面向對象---繼承 2011-12-17 10:09 tb
          學習了  回復  更多評論
            

          # re: js面向對象---繼承 2011-12-17 15:57 王鵬飛
          用js自己定義一個類,在項目中還沒用過。用的現成的ext提供的類  回復  更多評論
            

          # re: js面向對象---繼承 2011-12-20 10:12 李秋雨
          謝謝分享!  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 北流市| 汽车| 宁都县| 类乌齐县| 文水县| 中山市| 洪洞县| 平昌县| 南投市| 四平市| 鹤峰县| 苍梧县| 仁寿县| 蒲江县| 桃源县| 遵义县| 阳西县| 政和县| 介休市| 泾阳县| 揭东县| 湖北省| 突泉县| 华坪县| 新干县| 巴楚县| 奉节县| 亳州市| 山丹县| 启东市| 从化市| 天气| 南江县| 银川市| 繁峙县| 丹东市| 逊克县| 瓦房店市| 松原市| 阜宁县| 慈溪市|