SoFunction
Updated on 2025-04-13

Code that uses constructors to implement inheritance in JavaScript


// Create the parent class first
function Person(name, age, address) {
= name;
= age;
= address;
}
//Create subclass
function Student(score) {
= score;
//The constructor of the function can be called using the call method or the apply method
//Calling the constructor of the parent class, and calling the constructor of the Person class through the call method. This will initialize the Person object in the student, and the student will have a copy of the Person property.
(this,"zhangsan",22,"Beijing, China!");
}

var student = new Student(100);
alert( + + "point");