Blogger Information
Blog 64
fans 2
comment 1
visits 46333
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
深入理解js变量于函数基础
Y的博客
Original
606 people have browsed it

1. 实例演示变量与常量的区别

  1. // 1.变量:禁止重复命名,可以被更新(命名时用小驼峰命名)
  2. // 声明
  3. let userName;
  4. console.log(userName);
  5. // 声明并初始化(第一次赋值)
  6. let itemName = "时尚";
  7. console.log(itemName);
  8. // 更新
  9. userName = "小小";
  10. console.log(userName);
  11. // 2.常量:声明时必须初始化,声明后不能被更新和删除(常量命名时一般用大写)
  12. const ID = 123;
  13. console.log(ID);

2. 函数与匿名函数的区别

  1. // 函数
  2. function getName(name) {
  3. return "Welcome to" + name;
  4. };
  5. console.log(getName("天猫"));
  6. // 匿名函数:没有函数名的函数(可以解决函数声明提升)
  7. // console.log(sum(1, 2));
  8. let sum = function(a, b) {
  9. return a + b;
  10. };
  11. console.log(sum(1, 2));

3. 箭头函数的参数特征

  1. let sum = function (a, b) {
  2. return a + b;
  3. };
  4. // 用箭头函数简写(箭头函数只能用来匿名函数声明)
  5. sum = (a, b) => {
  6. return a + b;
  7. };
  8. console.log(sum(3, 4));
  9. // 如果函数只有一条语句可以不写return
  10. sum = (a, b) => a + b;
  11. console.log(sum(32, 42));
  12. // 如果只有一个参数小括号可以不用
  13. let tips = str => console.log(str);
  14. tips("欢迎大家");
  15. // 如果没有参数,小括号不能省略
  16. tips = () => console.log("success");
  17. console.log(tips("欢迎大家"));

4. 闭包原理与实现并演示它

  1. // 闭包
  2. // 自由变量: 即不是函数参数变量也不是私有变量,存在于函数调用上下文中
  3. // 闭包: 能够访问自由变量的函数,所以理论上讲,任何函数都是闭包
  4. let num = 100;
  5. function add(a, b) {
  6. // a,b是参数变量
  7. // c是私有变量
  8. // num是自由变量
  9. let c = 3;
  10. return a + b + c + num;
  11. };
  12. console.log(add(2, 3));
  13. function f() {
  14. let n = 100;
  15. // 这个返回的子函数就是闭包
  16. return function () {
  17. return n;
  18. };
  19. };
  20. // console.log(n);
  21. console.log(f()());
  22. function f1() {
  23. let a = 1;
  24. // a 相对于f1是私有变量,但是相对于返回的匿名函数就是一个自由变量
  25. return function () {
  26. return a++;
  27. };
  28. };
  29. let f2 = f1();
  30. console.log(f2());

5. 四种高阶函数

  1. // 1. 回调函数
  2. document.addEventListener("click", function() {
  3. alert("Hello Word");
  4. })
  5. // 2.偏函数
  6. let sum = function(a, b) {
  7. return function(c, d) {
  8. return a + b + c + d;
  9. };
  10. };
  11. let f1 = sum(1, 2);
  12. console.log(f1(3, 4));
  13. // 3.柯里化
  14. sum = function(a) {
  15. return function(b) {
  16. return function(c) {
  17. return function(d) {
  18. return a + b + c + d;
  19. };
  20. };
  21. };
  22. };
  23. let res = sum(1)(2)(3)(4);
  24. console.log(res);
  25. // 4.纯函数
  26. function add(a, b) {
  27. console.log(a + b);
  28. }
  29. add(3, 3);
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!