<div class="codetitle"> <span><a style="CURSOR: pointer" data="16721" class="copybut" id="copybut16721" onclick="doCopy('code16721')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code16721"> <br><script type="text/javascript"> <BR>//使用apply方法實作物件繼承<br><br>function Parent(username) { <BR>this.username = username; <BR>this.sayHello = function() { <BR>alert(this.username); <this.username); <BR>} <BR>} <br><br>function Child(username, password) { <BR>Parent.apply(this, new Array(username)); <BR>//和下面一樣<BR>//Parent .apply(this, [username]); <br><br>this.password = password; <br><br>this.sayWorld = function() { <BR>alert(this.password); <BR>} <BR>} <BR>var parent = new Parent("zhangsan"); <BR>var child = new Child("lisi", "123"); <br><br>parent.sayHello(); <BR>child.sayHello(); <BR>child.sayWorld(); <br><br></script> <br> </div>