手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >javascript中最常用的继承模式 组合继承
javascript中最常用的继承模式 组合继承
摘要:复制代码代码如下://创建基类functionPerson(name,age){this.name=name;this.age=age;}/...

复制代码 代码如下:

<script type="text/javascript">

//创建基类

function Person(name, age) {

this.name = name;

this.age = age;

}

//通过原型方式给基类添加函数(这样可以服用此函数)

Person.prototype.showName = function () {

alert(this.name);

}

//创建子类

function Student(name, age, score) {

this.score = score;

Person.call(this,name,age);

}

//把父类的实例赋值给子类的原型

Student.prototype = new Person();

//通过原型方式给子类添加函数(这样可以服用此函数)

Student.prototype.showScore = function () {

alert(this.score);

}

//以下为使用

var student = new Student("zhangsan", 22, 100);

student.showName();

student.showScore();

var stu = new Student("lisi", 25, 200);

stu.showName();

stu.showScore();

</script>

【javascript中最常用的继承模式 组合继承】相关文章:

javascript操作ul中li的方法

JavaScript中指定函数名称的相关方法

浅谈javascript中的闭包

javascript中DOM复选框选择用法实例

在JavaScript中使用NaN值的方法

javascript动态创建链接的方法

JavaScript正则表达式的分组匹配详解

javascript常用方法总结

简单谈谈javascript中this的隐式绑定

删除javascript所创建子节点的方法

精品推荐
分类导航