9.原型.html 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. /**
  11. * 所有构造函数都有个phototype属性 这个属性是该函数的圆形队形
  12. * 原型对象的特点:
  13. *
  14. * 属性 构造函数里
  15. * 方法 原型对象下
  16. */
  17. function Person(name,age) {
  18. this.name = name;
  19. this.age = age;
  20. // console.log(this,this.name,this.age);
  21. }
  22. Person.prototype.eat = function() {
  23. console.log("吃饭");
  24. }
  25. let aa = new Person("孙悟空",10);
  26. // console.log(Person.prototype.constructor)
  27. // aa.eat();
  28. /**
  29. * 子类继承父类
  30. * 1.在子类的 构造函数里 调用父类.call()方法
  31. * 2.子类的原型对象 = new 父类的继承方法
  32. */
  33. function Child(name,age) {
  34. Person.call(this,name,age);
  35. }
  36. Child.prototype = new Person();
  37. //实例化
  38. let bb = new Child("猪八戒",20);
  39. console.log(bb.name,bb.age);
  40. </script>
  41. </body>
  42. </html>