实现继承的时候,子类是必须有父类的所有属性和方法的,但子类的属性是手动添加上的,父类的方法不用手动添加到子类的方法吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Document</title> </head> <body> <script> // 父类 function People(name, age, sex) { this .name = name; this .age = age; this .sex = sex; }; People.prototype.sayHello = function () { console.log( '我是' + this .name + ',我今年' + this .age); }; People.prototype.sleep = function () { console.log( this .name + '开始睡觉' ); }; // 子类 function Student(name, age, sex, school, id) { this .name = name; this .age = age; this .sex = sex; this .school = school; this .id = id; }; // 实现继承 Student.prototype = new People(); Student.prototype.study = function () { console.log( '我正在学习' ); }; Student.prototype.exam = function () { console.log( '我正在考试' ); }; var hanmeimei = new Student( '韩梅梅' , 20, '女' ); hanmeimei.sayHello(); hanmeimei.sleep(); </script> </body> </html> |
18
收起
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧