<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 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    JS繼承

    Posted on 2012-08-13 19:29 mr.zhao 閱讀(185) 評論(0)  編輯  收藏
    js繼承有5種實現(xiàn)方式:
    1、繼承第一種方式:對象冒充
      function Parent(username){
        this.username = username;
        this.hello = function(){
          alert(this.username);
        }
      }
      function Child(username,password){
        //通過以下3行實現(xiàn)將Parent的屬性和方法追加到Child中,從而實現(xiàn)繼承
        //第一步:this.method是作為一個臨時的屬性,并且指向Parent所指向的對象,
        //第二步:執(zhí)行this.method方法,即執(zhí)行Parent所指向的對象函數(shù)
        //第三步:銷毀this.method屬性,即此時Child就已經(jīng)擁有了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方法的第一個參數(shù)的值賦值給類(即方法)中出現(xiàn)的this
      call方法的第二個參數(shù)開始依次賦值給類(即方法)所接受的參數(shù)

      function test(str){
        alert(this.name + " " + str);
      }
      var object = new Object();
      object.name = "zhangsan";
      test.call(object,"langsin");//此時,第一個參數(shù)值object傳遞給了test類(即方法)中出現(xiàn)的this,而第二個參數(shù)"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個參數(shù),
        A、第一個參數(shù)與call方法的第一個參數(shù)一樣,即賦值給類(即方法)中出現(xiàn)的this
        B、第二個參數(shù)為數(shù)組類型,這個數(shù)組中的每個元素依次賦值給類(即方法)所接受的參數(shù)

      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,從而實現(xiàn)了繼承
      function Person(){
      }
      Person.prototype.hello = "hello";
      Person.prototype.sayHello = function(){
        alert(this.hello);
      }
     
      function Child(){
      }
      Child.prototype = new Person();//這行的作用是:將Parent中將所有通過prototype追加的屬性和方法都追加到Child,從而實現(xiàn)了繼承
      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();

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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 四虎免费影院ww4164h| 十八禁视频在线观看免费无码无遮挡骂过 | 国产亚洲精品一品区99热| 一级毛片aa高清免费观看| 亚洲男人av香蕉爽爽爽爽| 思思久久99热免费精品6| 亚洲人成无码网WWW| eeuss免费天堂影院| 精品国产亚洲一区二区在线观看| 免费大片av手机看片| 亚洲人成网站色在线入口| 人人公开免费超级碰碰碰视频| 免费在线观看视频a| 免费看一级一级人妻片| 亚洲午夜国产精品无码老牛影视 | 日韩精品视频在线观看免费| 亚洲国产人成中文幕一级二级| 亚洲黄片手机免费观看| 久久亚洲精品中文字幕无码| www视频免费看| 亚洲爆乳少妇无码激情| 亚洲综合激情另类专区| 污污网站18禁在线永久免费观看| 亚洲人成网站在线观看播放动漫| 天天天欲色欲色WWW免费| 日韩在线观看视频免费| 久久久综合亚洲色一区二区三区 | mm1313亚洲精品国产| 中文字幕免费在线播放| 亚洲精品高清国产麻豆专区| 免费a级毛片无码a∨蜜芽试看| 美女露100%胸无遮挡免费观看| 国产午夜亚洲不卡| 国产成人福利免费视频| 成年免费a级毛片| 精品日韩亚洲AV无码一区二区三区 | 1区1区3区4区产品亚洲| 在线观看亚洲免费视频| 成人久久免费网站| 一本色道久久88亚洲精品综合| 亚洲国产精品一区二区第一页免 |