<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    用代碼來充實生活

    夢想遙不可及
    posts - 3, comments - 0, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    JS繼承

    Posted on 2012-08-13 19:29 mr.zhao 閱讀(186) 評論(0)  編輯  收藏
    js繼承有5種實現方式:
    1、繼承第一種方式:對象冒充
      function Parent(username){
        this.username = username;
        this.hello = function(){
          alert(this.username);
        }
      }
      function Child(username,password){
        //通過以下3行實現將Parent的屬性和方法追加到Child中,從而實現繼承
        //第一步:this.method是作為一個臨時的屬性,并且指向Parent所指向的對象,
        //第二步:執行this.method方法,即執行Parent所指向的對象函數
        //第三步:銷毀this.method屬性,即此時Child就已經擁有了Parent的所有屬性和方法
        this.method = Parent;
        this.method(username);//最關鍵的一行
        delete this.method;

        this.password = password;
        this.world = function(){
          alert(this.password);
        }
      }
      var parent = new Parent("zhangsan");
      var child = new Child("lisi","123456");
      parent.hello();
      child.hello();
      child.world();

    2、繼承第二種方式:call()方法方式
      call方法是Function類中的方法
      call方法的第一個參數的值賦值給類(即方法)中出現的this
      call方法的第二個參數開始依次賦值給類(即方法)所接受的參數

      function test(str){
        alert(this.name + " " + str);
      }
      var object = new Object();
      object.name = "zhangsan";
      test.call(object,"langsin");//此時,第一個參數值object傳遞給了test類(即方法)中出現的this,而第二個參數"langsin"則賦值給了test類(即方法)的str

      function Parent(username){
        this.username = username;
        this.hello = function(){
          alert(this.username);
        }
      }
      function Child(username,password){
        Parent.call(this,username);
       
        this.password = password;
        this.world = function(){
          alert(this.password);
        }
      }
      var parent = new Parent("zhangsan");
      var child = new Child("lisi","123456");
      parent.hello();
      child.hello();
      child.world();

    3、繼承的第三種方式:apply()方法方式
      apply方法接受2個參數,
        A、第一個參數與call方法的第一個參數一樣,即賦值給類(即方法)中出現的this
        B、第二個參數為數組類型,這個數組中的每個元素依次賦值給類(即方法)所接受的參數

      function Parent(username){
        this.username = username;
        this.hello = function(){
          alert(this.username);
        }
      }
      function Child(username,password){
        Parent.apply(this,new Array(username));
       
        this.password = password;
        this.world = function(){
          alert(this.password);
        }
      }
      var parent = new Parent("zhangsan");
      var child = new Child("lisi","123456");
      parent.hello();
      child.hello();
      child.world();

    4、繼承的第四種方式:原型鏈方式,即子類通過prototype將所有在父類中通過prototype追加的屬性和方法都追加到Child,從而實現了繼承
      function Person(){
      }
      Person.prototype.hello = "hello";
      Person.prototype.sayHello = function(){
        alert(this.hello);
      }
     
      function Child(){
      }
      Child.prototype = new Person();//這行的作用是:將Parent中將所有通過prototype追加的屬性和方法都追加到Child,從而實現了繼承
      Child.prototype.world = "world";
      Child.prototype.sayWorld = function(){
        alert(this.world);
      }
     
      var c = new Child();
      c.sayHello();
      c.sayWorld();

    5、繼承的第五種方式:混合方式
      混合了call方式、原型鏈方式

      function Parent(hello){
        this.hello = hello;
      }
      Parent.prototype.sayHello = function(){
        alert(this.hello);
      }

      function Child(hello,world){
        Parent.call(this,hello);//將父類的屬性繼承過來
        this.world = world;//新增一些屬性
      }

      Child.prototype = new Parent();//將父類的方法繼承過來

      Child.prototype.sayWorld = function(){//新增一些方法
        alert(this.world);
      }

      var c = new Child("zhangsan","lisi");
      c.sayHello();
      c.sayWorld();

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


    網站導航:
     
    主站蜘蛛池模板: 中国极品美軳免费观看| 国产精品亚洲精品日韩电影| 你是我的城池营垒免费看| 日韩亚洲精品福利| 精品一区二区三区免费毛片| 欧洲美熟女乱又伦免费视频| 在线aⅴ亚洲中文字幕| 大学生一级毛片免费看| 亚洲人成人77777在线播放| 午夜免费福利视频| 亚洲国产中文在线二区三区免| 午夜视频在线免费观看| 久久亚洲AV成人出白浆无码国产| 91精品国产免费| 亚洲H在线播放在线观看H| 免费视频中文字幕| 理论片在线观看免费| 久久影视综合亚洲| 无码国产精品一区二区免费模式| 噜噜噜亚洲色成人网站∨| 岛国av无码免费无禁网站| 看Aⅴ免费毛片手机播放| 国产亚洲人成网站在线观看| 亚洲免费在线播放| 亚洲国产精品日韩在线观看| 女人张开腿等男人桶免费视频| 国产精品亚洲精品久久精品| 亚洲女初尝黑人巨高清| 91香焦国产线观看看免费| 亚洲人成人网毛片在线播放| 四虎永久免费地址在线网站| 中文字幕不卡高清免费| 亚洲精彩视频在线观看| 日本人的色道www免费一区| 两性色午夜免费视频| 亚洲另类精品xxxx人妖| 四虎免费永久在线播放| AAA日本高清在线播放免费观看| 亚洲av日韩av综合| 久久久久亚洲AV成人网人人网站 | 久久精品成人免费网站|