Home > Web Front-end > JS Tutorial > Code using constructors to implement inheritance in JavaScript_js object-oriented

Code using constructors to implement inheritance in JavaScript_js object-oriented

WBOY
Release: 2016-05-16 18:21:26
Original
980 people have browsed it
Copy code The code is as follows:

//First create the parent class
function Person(name, age , address) {
this.name = name;
this.age = age;
this.address = address;
}
//Create a subclass
function Student(score ) {
this.score = score;
//You can use the call method or the apply method to call the constructor of the function
//Call the constructor of the parent class, and call the constructor of the Person class through the call method . This will initialize the Person object in the student, and the student will also have a copy of the Person's attributes
Person.call(this, "zhangsan",22, "Beijing, China!");
}

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


//The second in the above Person.call method call parameters start with the passed data parameters
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template