<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); <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>