22.类.html 1.0 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. /**
  11. * 构造函数
  12. * new 实例化对象
  13. * this指向当前对象本身
  14. * 首字母大写
  15. * 不使用return
  16. * 自带了prototype 原型对象 和 constructor 构造器
  17. *
  18. * 属性 写在构造函数下
  19. * 方法 写在原型对象下
  20. *
  21. */
  22. function Person(name,age) {
  23. console.log(this);
  24. this.name = name;
  25. this.age = age;
  26. }
  27. Person.prototype.eat = function() {
  28. console.log("吃粽子")
  29. }
  30. Person.prototype.drink = function() {
  31. console.log("喝甜水")
  32. }
  33. var p1 = new Person('Lucy',20);
  34. console.log(p1.name,p1.age);
  35. p1.eat();
  36. p1.drink();
  37. p1.name = '哈哈';
  38. console.log(p1);
  39. </script>
  40. </body>
  41. </html>