文章目录
- 1. js作用域
- 2. 函数
- 3. 解构赋值
 
 
 
1. js作用域
作用域规定了变量能够呗访问的范围,离开这个范围,这个变量就不能够被访问到。
-  局部作用域:可以分为函数作用域和块作用域 -  块作用域,let 声明会产生块级作用域,var 声明不会产生作用域 <!DOCTYPE html> <html> <head><title></title> </head> <body></body> <script type="text/javascript">if(true){var a = 10;}console.log(a); </script> </html>控制台上输出:10 
 
-  
-  全局作用域 
-  作用域链:底层就是变量查找机制 - 在函数被执行时,会优先查找当前函数作用域中查找变量
- 如果当前作用域找不到则会依次逐级查找父级作用域直到全局作用域
 
-  js垃圾回收机制(简称gc) - 生命周期:内存分配、内存使用、内存回收
- 全局变量一般不会被回收的
- 一般情况下局部变量的值,不用了,会被自动回收
- 内存泄漏:程序中分配的内存由于某种原因程序未释放或无法释放叫做内存泄漏
 
-  闭包: -  function fn(){let a = 10;return function(){return a;} }
-  外部也可以访问函数内部的变量 
 
-  
-  变量提升 -  把所以var声明的变量提升到当前作用域的最前面 
-  只提升声明,不提升赋值 
-  console.log(a); var a = 10; // 结果为undefined
-  上述代码相当于 
-  var a console.log(a) a = 10
 
-  
2. 函数
-  函数提升 -  fn() function fn(){console.log('函数提升'); } // 打印 函数提升
-  只提升函数声明,不提升函数调用 
-  fun() var fun = function(){console.log('函数表达式') } // 报错
-  函数表达式必须先声明,后调用 
 
-  
-  函数参数 -  动态参数 -  arguments 动态参数,只存在于函数里面,是个伪数组 
-  function sum(){let v = 0;for(let e of arguments){v += e;}return v; } console.log(sum(1,2,3)); console.log(sum(1,2,3,4)); // 6 // 10
 
-  
-  剩余参数 -  function fun(...args){let v = 0;for(let e of args){v += e;}return v; } console.log(fun(1,2,3)); console.log(fun(1,2,3,4)); // 6 // 10
 
-  
 
-  
-  箭头函数:更简短的函数写法并且不绑定this,箭头函数的写法比函数表达书更简洁 -  const fn = ()=>{console.log(123); }
-  只有一个形参的时候,可以省略小括号 
-  const fn = x=>{console.log(x); }
-  只有一行代码的时候,可以省略大括号 
-  const fn = x=>console.log(x);
-  只有一行代码,可以省略return 
-  const fn = x => x+x; // 相当于如下 const fn = (x)=>{return x + x; }
-  箭头函数没有arguments动态参数,但是有剩余参数 …args 
-  this的指向问题 -  普通 
-  console.log(this); // 指向window function fn(){console.log(this); // 指向window } window.fn(); const obj = {hello:function(){console.log(this);} } obj.hello(); // this 指向obj这个对象
-  箭头函数 
-  const fn = ()=>{console.log(this); } // 箭头函数的this指向其上一层的this fn(); // 结果为windowconst obj = {hello:()=>{console.log(this);} } obj.hello(); // this指向window // 箭头函数中没有this,找上一层this的指向
 
-  
 
-  
3. 解构赋值
-  数组结构:将数组的单元值快速批量赋值给一系列变量的简洁语法 -  const [a,b,c] = [1,2,3]; console.log(a,b,c); // 1,2,3
-  应用:变量交换 var a = 1,b=2; [a,b] = [b,a]; console.log(a,b); // 2,1
-  剩余参数 const [a,b,...c] = [1,2,3,4]; console.log(a,b,c); // 1,2,[3,4]
-  防止undefined const [a=0,b=1]=[1,2]; console.log(a,b); // 1,2
-  忽略某个值 const [a,,c] = [1,2,3]; console.log(a,c); // 1,3
 
-  
-  对象结构:将对象属性和方法快速批量赋值给一系列变量的简洁语法 -  const obj = {username:'liuze',age:23,address:'湖南长沙' } // 对象结构 const {username,age,address} = obj; console.log(username,age,address); // liuze 23 湖南长沙
-  对象属性的值将被赋值给与属性名相同的变量 
-  对象的结构的变量名可以重新改名 旧变量名:新变量名 
-  const {username:uname,age,address} = obj; console.log(uname,age,address); // liuze 23 湖南长沙
-  多层对象的结构 
-  const obj = {obj1:{uname:'liuze',age:23} } const {obj1:{uname,age}} = obj; console.log(uname,age); // liuze 23
 
-