Blogger Information
Blog 41
fans 2
comment 0
visits 29718
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js之作用域和闭包与原型
月光下,遗忘黑暗
Original
483 people have browsed it

1.作用域

代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>作用域</title>
  6. </head>
  7. <body>
  8. <script>
  9. // 全局作用域
  10. let a = 1;
  11. // 函数作用域
  12. let fuc =()=> {
  13. let c =2;
  14. return c;
  15. }
  16. console.log(fuc());
  17. // console.log(c);
  18. // 块作用域
  19. {
  20. let b = 1;
  21. }
  22. console.log(b);
  23. </script>
  24. </body>
  25. </html>

效果图

2.闭包

代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>闭包</title>
  6. </head>
  7. <body>
  8. <script>
  9. let a = 1;
  10. let sum = (b,c) => {
  11. // b,c是形参,是自有的参数,c是自由变量,出现自由变量即为闭包
  12. return a+b+c;
  13. }
  14. console.log(sum(2,3));
  15. </script>
  16. </body>
  17. </html>

效果图

3.原型与继承

代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>原型与继承</title>
  6. </head>
  7. <body>
  8. <script>
  9. function User(name) {
  10. this.name = name;
  11. // show()输出属性
  12. }
  13. // 构造函数要加new
  14. const user = new User('root');
  15. console.log(user,typeof user);
  16. // user对象的原型属性永远指向他的构造函数的原型属性对象
  17. // user对象的原型
  18. console.log(user.__proto__)
  19. // user的构造函数的原型
  20. console.log(User.prototype);
  21. // 构造函数模拟类
  22. // 构造函数对象的原型对象上的成员,可以被所有实例共享
  23. User.prototype.show = function() {
  24. return {name: this.name}
  25. }
  26. console.log(user.show())
  27. const user1 = new User('admin');
  28. // 这时show方法已经搬到了user的原型上了,并不在user里面了
  29. console.log(user1);
  30. console.log(user1.show());
  31. class User1 {
  32. //构造函数
  33. constructor(a,b) {
  34. this.a = a;
  35. this.b = b;
  36. }
  37. // 原型方法(公共方法)
  38. show () {
  39. return {
  40. a: this.a , b:this.b
  41. }
  42. }
  43. // 静态方法
  44. static fetch () {
  45. return 'hello world!!!';
  46. }
  47. // 静态属性
  48. static me = '陈抟';
  49. //私有成员
  50. #age = 12;
  51. // 使用访问器属性
  52. get age() {
  53. return this.#age;
  54. }
  55. set age(value) {
  56. this.#age = value;
  57. }
  58. }
  59. const a = new User1(1,2);
  60. console.log(a.show());
  61. // 只有静态方法和静态属性可以直接用对象访问
  62. console.log(User1.fetch());
  63. console.log(User1.me);
  64. a.age = 100;
  65. console.log(a.age)
  66. // 继承
  67. class User2 extends User1 {
  68. // 第一步必须将父类的构造方法来执行以下,否者this用不了
  69. constructor(a,b,c) {
  70. super(a,b);
  71. // 第二步给子类的新成员赋值
  72. this.c = c;
  73. }
  74. // 父类的原型方法
  75. show () {
  76. return {
  77. a: this.a , b:this.b,c:this.c
  78. };
  79. }
  80. }
  81. const aaa = new User2(1,2,3);
  82. console.log(aaa.show());
  83. </script>
  84. </body>
  85. </html>

效果图

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post