<div class="codetitle"> <span><a style="CURSOR: pointer" data="10479" class="copybut" id="copybut10479" onclick="doCopy('code10479')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code10479"> <br><script type="text/javascript"> <br>//创建基类 <br>function Person(name, age) { <br>this.name = name; <br>this.age = age; <br>} <br>//通过原型方式给基类添加函数(这样可以服用此函数) <br>Person.prototype.showName = function () { <br>alert(this.name); <br>} <br>//创建子类 <br>function Student(name, age, score) { <br>this.score = score; <br>Person.call(this,name,age); <br>} <br>//把父类的实例赋值给子类的原型 <br>Student.prototype = new Person(); <br>//通过原型方式给子类添加函数(这样可以服用此函数) <br>Student.prototype.showScore = function () { <br>alert(this.score); <br>} <br><br>//以下为使用 <br>var student = new Student("zhangsan", 22, 100); <br>student.showName(); <br>student.showScore(); <br><br>var stu = new Student("lisi", 25, 200); <br>stu.showName(); <br>stu.showScore(); <br></script> <br> </div>