12.类.html 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. function Person(name, age) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. /**
  15. * 构造函数中 写属性
  16. * 原型中 写方法
  17. */
  18. Person.prototype.eat = function () {
  19. console.log("今天吃了好多");
  20. };
  21. var p1 = new Person("Lucy", 18);
  22. p1.eat();
  23. //constructor 构造器
  24. /**
  25. * 继承父类的方法(继承方法)
  26. * 1.子类构造函数里: 通过调用父类的.call继承
  27. * 2.子类的继承对象 = new 父类
  28. */
  29. console.log(Person.prototype.constructor)
  30. function Fn1(name,age) {
  31. Person.call(this,name,age);
  32. // Person.apply(this,[name,age]);
  33. }
  34. Fn1.prototype = new Person()
  35. Fn1.prototype.constructor = Fn1;
  36. var p2 = new Fn1("小明",90);
  37. console.log(p2);
  38. p2.eat();
  39. </script>
  40. </body>
  41. </html>