组合继承
<!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>组合继承</title> </head> <body> <script> function Person(name, age) { this.name = name; this.age = age; } Person.prototype.setName = function (name) { console.log(this.name); this.name = name; }; function Student(name, age, price) { Person.call(this, name, age); this.price = price; } Student.prototype = new Person(); //为了看到父类型的方法 Student.prototype.constructor = Student; //修正constructor属性 Student.prototype.setPrice = function (price) { this.price = price; }; var stu = new Student("张三", 18, 100); stu.setName("李四"); stu.setPrice(200); console.log(stu.name, stu.age, stu.price); console.dir(stu); </script> </body> </html>
标签:name,组合,继承,price,JS,stu,Student,prototype,age From: https://www.cnblogs.com/malong1992/p/17413531.html