12.借用构造函数继承(假的).html 1010 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>借用构造函数继承</title>
  8. </head>
  9. <body>
  10. <!--
  11. 方式二:借用构造函数继承(假的)
  12. 1、套路
  13. 1、定义父类型构造函数
  14. 2、定义子类型构造函数
  15. 3、在子类型构造函数中调用父类型构造
  16. 2、关键
  17. 在子类型构造函数中通过call()调用父类型构造函数
  18. -->
  19. <script>
  20. function Person(name, age) {
  21. this.name = name
  22. this.age = age
  23. }
  24. function Student(name, age, price) {
  25. Person.call(this, name, age) //相当于:this.Person(name,age)
  26. this.price = price
  27. }
  28. var s = new Student('小二', 18, 800)
  29. console.log(s.name, s.age, s.price)
  30. </script>
  31. </body>
  32. </html>