12345678910111213141516171819202122232425262728293031323334353637 |
- <!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、套路
- 1、定义父类型构造函数
- 2、定义子类型构造函数
- 3、在子类型构造函数中调用父类型构造
- 2、关键
- 在子类型构造函数中通过call()调用父类型构造函数
- -->
- <script>
- function Person(name, age) {
- this.name = name
- this.age = age
- }
- function Student(name, age, price) {
- Person.call(this, name, age) //相当于:this.Person(name,age)
- this.price = price
- }
- var s = new Student('小二', 18, 800)
- console.log(s.name, s.age, s.price)
- </script>
- </body>
- </html>
|