22.原型和原型链.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 fn1() {
  11. console.log(this, "this"); // window
  12. return 1;
  13. }
  14. let a = fn1();
  15. // let xxx = function(){};
  16. // (function(){})()
  17. console.log(a, "a");
  18. /**
  19. * 构造函数:
  20. * 1.this指向当前对象本身
  21. * 2.首字母大写
  22. * 3.返回值不用return
  23. * 4.使用时 必须通过new调用
  24. *
  25. *
  26. * 属性 写在构造函数下
  27. * 方法 写在原型下
  28. */
  29. function Person(a, b) {
  30. this.name = a;
  31. this.age = b;
  32. console.log("person", this);
  33. }
  34. Person.prototype.eat = function() {
  35. console.log(this.name + '该吃饭了')
  36. }
  37. // 实例化对象
  38. let p = new Person("瑶一瑶", 4);
  39. console.log(p);
  40. p.eat()
  41. /**
  42. * 原型:
  43. * 1.构造函数中自带了prototype(显性)属性 指向 当前的原型
  44. * 2.构造函数中自带了一个属性constructor(构造器) 指向prototype的构造函数
  45. * 3.当前实例化对象中自带一个_proto_(隐性)属性 指向当前构造函数原型
  46. */
  47. /**
  48. * 原型链:
  49. * 访问实例化对象,在对象本身属性查找
  50. * 通过_proto_(原型自带的一个隐型方法查找)去原型上找
  51. * 若还找不到 则在原型对象上的prototype上查找(Object)若找到返回 若没有则返回null
  52. */
  53. </script>
  54. </body>
  55. </html>