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