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 管先飛 閱讀(2184) 評論(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 李秋雨
          謝謝分享!  回復  更多評論
            


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


          網站導航:
           
          主站蜘蛛池模板: 青浦区| 遵义市| 福清市| 凤台县| 台东市| 博罗县| 湾仔区| 宁津县| 三原县| 双城市| 公安县| 若尔盖县| 克什克腾旗| 白银市| 东莞市| 洛隆县| 洛扎县| 乌苏市| 长治县| 青龙| 富蕴县| 凤冈县| 凌云县| 博罗县| 新邵县| 鸡东县| 肥乡县| 特克斯县| 安阳市| 精河县| 德化县| 南岸区| 黄山市| 筠连县| 托里县| 容城县| 临海市| 阳西县| 洪江市| 芦山县| 凤冈县|