12.类.html 974 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. <!--
  10. 函数 中 写属性
  11. 原型 中 写方法
  12. -->
  13. <script>
  14. function Person(name, age) {
  15. this.name = name;
  16. this.age = age;
  17. }
  18. Person.prototype.eat = function() {
  19. console.log("吃东西")
  20. }
  21. let p1 = new Person("暖宝宝", 10);
  22. console.log(p1);
  23. console.log(Person.prototype.constructor);
  24. /**
  25. * 继承父类的方法:
  26. * 1.子类的构造函数里:通过调用的父类.call(this,xxxx)
  27. * 2.子类的原型等于父类的实例化对象
  28. */
  29. function Fn1(name,age) {
  30. Person.apply(this,[name,age])
  31. }
  32. Fn1.prototype = new Person();
  33. var fn1 = new Fn1("小明",5);
  34. console.log(fn1)
  35. </script>
  36. </body>
  37. </html>