parent class derived subclass

 1 function Person(name, age){
 2     this.name = name
 3     this.age = age
 4 }
 5 Person.prototype.say = (name) => {
 6     console.log('person', name)
 7 }
 8 function Student(sex){
 9     Person.apply(this, arguments)
10     this.sex = sex
11 }
12 Student.prototype.say = (name) => {
13     console.log('student', name)
14 }
15 // Student.prototype = Person.prototype
16 // Student.prototype.constructor = Student
17 let F= function(){}
18 F.prototype = Person.prototype
19 Student.prototype = new F()
20 Student.prototype.constructor = Student
21 
22 let tom = new  Student('tom', 20, '男')
23 tom.say('hello') //person:hello

Disadvantages of the method:

The method of the parent class is called, and the subclass does not override it

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324894872&siteId=291194637