实现代码如下:

//首先创建父类
function Person(name, age, address) {
this.name = name;
this.age = age;
this.address = address;
}
//创建子类
function Student(score) {
this.score = score;
//可以用call方法或者是apply方法调用函数的构造函数
//调用父类的构造函数,通过call方法调用Person类的构造函数。这样就会在student中初始化Person对象,student也就有了Person的属性的副本
Person.call(this,"zhangsan",22,"中国北京!");
}

var student = new Student(100);
alert(student.address + student.score + "分");


//上述Person.call方法调用中第二个参数开始为传递的数据参数

以上就是【JavaScript中使用构造函数实现继承的代码】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)

与本文相关的软件

发表我的评论

最新评论

  1. 暂无评论