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

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

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

    TH889

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      2 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks
      1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      2<html>
      3 <head>
      4  <title> New Document </title>
      5  <meta name="Generator" content="EditPlus">
      6  <meta name="Author" content="">
      7  <meta name="Keywords" content="">
      8  <meta name="Description" content="">
      9  <script type = "text/javaScript">
     10  js繼承有5種實現方式: 
     111、繼承第一種方式:對象冒充 
     12  function Parent(username)
     13    this.username = username; 
     14    this.hello = function()
     15      alert(this.username); 
     16    }
     
     17  }
     
     18  function Child(username,password)
     19    //通過以下3行實現將Parent的屬性和方法追加到Child中,從而實現繼承 
     20    //第一步:this.method是作為一個臨時的屬性,并且指向Parent所指向的對象, 
     21    //第二步:執行this.method方法,即執行Parent所指向的對象函數 
     22    //第三步:銷毀this.method屬性,即此時Child就已經擁有了Parent的所有屬性和方法 
     23    this.method = Parent; 
     24    this.method(username);//最關鍵的一行 
     25    delete this.method; 
     26
     27    this.password = password; 
     28    this.world = function()
     29      alert(this.password); 
     30    }
     
     31  }
     
     32  var parent = new Parent("zhangsan"); 
     33  var child = new Child("lisi","123456"); 
     34  parent.hello(); 
     35  child.hello(); 
     36  child.world(); 
     37
     382、繼承第二種方式:call()方法方式 
     39  call方法是Function類中的方法 
     40  call方法的第一個參數的值賦值給類(即方法)中出現的this 
     41  call方法的第二個參數開始依次賦值給類(即方法)所接受的參數 
     42
     43  function test(str)
     44    alert(this.name + " " + str); 
     45  }
     
     46  var object = new Object(); 
     47  object.name = "zhangsan"
     48  test.call(object,"langsin");//此時,第一個參數值object傳遞給了test類(即方法)中出現的this,而第二個參數"langsin"則賦值給了test類(即方法)的str
     49 
     50  function Parent(username)
     51    this.username = username; 
     52    this.hello = function()
     53      alert(this.username); 
     54    }
     
     55  }
     
     56  function Child(username,password)
     57    Parent.call(this,username); 
     58    
     59    this.password = password; 
     60    this.world = function()
     61      alert(this.password); 
     62    }
     
     63  }
     
     64  var parent = new Parent("zhangsan"); 
     65  var child = new Child("lisi","123456"); 
     66  parent.hello(); 
     67  child.hello(); 
     68  child.world(); 
     69
     703、繼承的第三種方式:apply()方法方式 
     71  apply方法接受2個參數, 
     72    A、第一個參數與call方法的第一個參數一樣,即賦值給類(即方法)中出現的this 
     73    B、第二個參數為數組類型,這個數組中的每個元素依次賦值給類(即方法)所接受的參數 
     74
     75  function Parent(username)
     76    this.username = username; 
     77    this.hello = function()
     78      alert(this.username); 
     79    }
     
     80  }
     
     81  function Child(username,password)
     82    Parent.apply(this,new Array(username)); 
     83    
     84    this.password = password; 
     85    this.world = function()
     86      alert(this.password); 
     87    }
     
     88  }
     
     89  var parent = new Parent("zhangsan"); 
     90  var child = new Child("lisi","123456"); 
     91  parent.hello(); 
     92  child.hello(); 
     93  child.world(); 
     94
     954、繼承的第四種方式:原型鏈方式,即子類通過prototype將所有在父類中通過prototype追加的屬性和方法都追加到Child,從而實現了繼承 
     96  function Person()
     97  }
     
     98  Person.prototype.hello = "hello"
     99  Person.prototype.sayHello = function()
    100    alert(this.hello); 
    101  }
     
    102  
    103  function Child()
    104  }
     
    105  Child.prototype = new Person();//這行的作用是:將Parent中將所有通過prototype追加的屬性和方法都追加到Child,從而實現了繼承
    106   Child.prototype.world = "world"
    107  Child.prototype.sayWorld = function()
    108    alert(this.world); 
    109  }
     
    110  
    111  var c = new Child(); 
    112  c.sayHello(); 
    113  c.sayWorld(); 
    114
    1155、繼承的第五種方式:混合方式 
    116  混合了call方式、原型鏈方式 
    117
    118  function Parent(hello)
    119    this.hello = hello; 
    120  }
     
    121  Parent.prototype.sayHello = function()
    122    alert(this.hello); 
    123  }
     
    124
    125  function Child(hello,world)
    126    Parent.call(this,hello);//將父類的屬性繼承過來 
    127    this.world = world;//新增一些屬性 
    128  }
     
    129
    130  Child.prototype = new Parent();//將父類的方法繼承過來 
    131
    132  Child.prototype.sayWorld = function(){//新增一些方法 
    133    alert(this.world); 
    134  }
     
    135
    136  var c = new Child("zhangsan","lisi"); 
    137  c.sayHello(); 
    138  c.sayWorld(); 
    139 </script>
    140 </head>
    141 <body>
    142  
    143 </body>
    144</html>
    145
    posted on 2012-08-13 23:09 TH 閱讀(145) 評論(0)  編輯  收藏

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


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 99热精品在线免费观看| 亚洲精品亚洲人成在线麻豆| 无码一区二区三区免费视频| 在线免费观看伊人三级电影| 亚洲精品无码你懂的| 亚洲伊人久久精品| 亚洲另类激情综合偷自拍| 亚洲欧洲久久av| 四虎影视永久免费观看| 国内自产少妇自拍区免费| 0588影视手机免费看片| 99精品国产成人a∨免费看| 国产成人无码区免费网站| 九九综合VA免费看| 国产午夜亚洲精品不卡免下载 | 国产免费福利体检区久久| 国产成人亚洲精品91专区高清| 亚洲乱码无人区卡1卡2卡3| 亚洲剧场午夜在线观看| 亚洲免费观看在线视频| 亚洲最大黄色网址| 亚洲精品视频观看| 久久亚洲日韩精品一区二区三区| 国产亚洲欧洲精品| 亚洲不卡av不卡一区二区| 亚洲人成中文字幕在线观看| 国产成人精品久久亚洲| 亚洲欧洲一区二区三区| 亚洲高清免费视频| 国产亚洲大尺度无码无码专线| 亚洲精品视频在线观看你懂的| 亚洲国产专区一区| 国产成人亚洲精品狼色在线 | 国色精品va在线观看免费视频 | 亚洲综合区小说区激情区 | 精品无码国产污污污免费网站国产| 高h视频在线免费观看| 特级毛片免费观看视频| 精品人妻系列无码人妻免费视频| a级毛片无码免费真人久久| 成人精品一区二区三区不卡免费看|