20_类.html 1005 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. /*
  12. 1.构造函数可以创建实例化对象
  13. 2.属性写在 构造函数里
  14. 方法写在 原型对象下
  15. */
  16. //构造函数
  17. function person(name, age) {
  18. this.name = name
  19. this.age = age
  20. // this.eat = function () {
  21. // console.log(this.name + '我在吃饭')
  22. // }
  23. }
  24. //原型对象
  25. person.prototype.eat = function(){
  26. console.log(this.name + '我在吃饭')
  27. }
  28. /*
  29. 所有的构造函数都有一个prototype属性
  30. 这个prototype属性 指向的都是原型对象
  31. */
  32. //实例化对象
  33. var p1 = new person('zs', 18)
  34. var p2 = new person('lisi', 20)
  35. console.log(p1)
  36. console.log(p2)
  37. p1.eat()
  38. p2.eat()
  39. </script>
  40. </body>
  41. </html>