java學習

          java學習

           

          javascript面向對象中繼承的幾種方式

          <html>
              <head>
              
              </head>
              <body>
                  <script type="text/javascript">
                  //繼承之對象冒充方式,可以繼承多個父類
                      function user(name){
                          this.name = name;
                          this.sayname = function(){
                              alert(this.name);
                          }
                      }
                      function student(name,score){
                          //user(name);
                          this.temp = user;
                          this.temp(name);
                          delete this.temp;
                          this.score = score;
                          this.sayscore = function(){
                              alert(this.score);
                          }
                      }
                      var s = new student('tom',33);
                      //s.sayname();
                      //s.sayscore();
                      
                      //用call()函數實現繼承
                      function user(name){
                          this.name = name;
                          this.sayname = function(){
                              alert(this.name);
                          }
                      }
                      function student(name,score){
                          user.call(this,name);
                          this.score = score;
                          this.sayscore = function(){
                              alert(this.score);
                          }
                      }
                      var s = new student('tom',33);
                      //s.sayname();
                      //s.sayscore();
                      
                      //用apply()函數實現繼承
                      function user(name){
                          this.name = name;
                          this.sayname = function(){
                              alert(this.name);
                          }
                      }
                      function student(name,score){
                          user.apply(this,[name]);
                          this.score = score;
                          this.sayscore = function(){
                              alert(this.score);
                          }
                      }
                      var s = new student('tom',33);
                      //s.sayname();
                      //s.sayscore();
                      
                      
                      //原型鏈方式實現繼承
                      //1.將父類中所用通過prototype設置的屬性或方法放到子類中
                      //2.并覆蓋子類中所以的prototype的設置
                      //3.所以子類自己的所以的prototype的設置要放在繼承父類的下面
                      //4.缺點是不支持多個繼承,構造函數不能有參數
                      
                      function user(){}            
                      user.prototype.name = '';
                      user.prototype.say = function(){
                          alert(this.name);
                      }
                      function student(){}
                          student.prototype =new user();
                          student.prototype.age = 0;
                          student.prototype.show = function(){
                              alert(this.age);
                          }
                      
                      var s = new student();
                      s.name = 'tom';
                      s.age = 44;
                      //s.say();
                      //s.show();
                      //alert(s instanceof user);
                      //alert(s instanceof student);
                      
                      
                      //混合模式實現繼承
                      function user(name){
                          this.name = name;
                      }
                      user.prototype.sayname = function(){
                          alert(this.name);
                      }
                      function student(name){
                          user.call(this.name);
                      }
                      //將user中所有通過prototype設置的方法放到student中
                      student.prototype = new user();
                      student.prototype.age = 0;
                      student.prototype.sayage = function(){
                          alert(this.age);
                      }
                      var s = new student();
                      s.name = 'tom';
                      s.age = 44;
                      s.sayname();
                      s.sayage();
                      alert(s instanceof user);
                      alert(s instanceof student);
                      
                  </script>
              </body>
          <html/>

          posted on 2013-06-17 15:26 楊軍威 閱讀(369) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 怀仁县| 连江县| 桂阳县| 涿鹿县| 乌兰察布市| 昭平县| 兴文县| 焉耆| 五大连池市| 梁山县| 岫岩| 民丰县| 西平县| 洪湖市| 罗山县| 天峻县| 祥云县| 揭西县| 资中县| 胶南市| 长岛县| 霍州市| 故城县| 寻甸| 普宁市| 万年县| 望奎县| 叶城县| 闸北区| 德昌县| 盘山县| 那坡县| 东台市| 新兴县| 错那县| 松原市| 嘉祥县| 博罗县| 澄江县| 黔东| 读书|