//创建基类 function Person(name, age) { this.name = name; this.age = age; } //通过原型方式给基类添加函数(这样可以服用此函数) Person.prototype.sh" />

广州明生堂生物科技有限公司


javascript中最常用的继承模式 组合继承

网络编程 javascript中最常用的继承模式 组合继承 06-22

<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最常用与实用的创建类的代码
//以构造函数方式添加私有属性和方法functionPerson(name,age,address){this.name=name;this.age=age;this.address=address;}//以原型方式添加公有属性、方法Person.prototype={const

JavaScript中使用构造函数实现继承的代码
//首先创建父类functionPerson(name,age,address){this.name=name;this.age=age;this.address=address;}//创建子类functionStudent(score){this.score=score;//可以用call方法或者是apply方法

JavaScript类和继承 prototype属性
我们已经在第一章中使用prototype属性模拟类和继承的实现。prototype属性本质上还是一个JavaScript对象。并且每个函数都有一个默认的prototype属性。如果这


编辑:广州明生堂生物科技有限公司

标签:函数,子类,属性,原型,方式