Home  >  Article  >  Web Front-end  >  JavaScript中使用构造函数实现继承的代码_js面向对象

JavaScript中使用构造函数实现继承的代码_js面向对象

WBOY
WBOYOriginal
2016-05-16 18:21:26893browse
复制代码 代码如下:

//首先创建父类
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方法调用中第二个参数开始为传递的数据参数
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn