12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <!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>
- <!--
- 方式三:原型链+借用构造函数的组合继承
- 1、利用原型链实现对父类型对象的方法继承
- 2、利用supper()借用父类型构建函数初始化相同属性
- -->
- <script>
- function Person(name, age) {
- this.name = name
- this.age = age
- }
- Person.prototype.setName = function(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 s = new Student('张三',22,800)
- s.setName('李四')
- s.setPrice(900)
- console.log(s.name,s.age,s.price)
- </script>
- </body>
- </html>
|