Let 具有块级做用域
(1)没有预解析,不存在变量提高chrome
Let a=12; function show(){ console.log(a); //Tdz 开始暂时性死区, let a=5; //TDZ结束 }
(2)同一做用域不能重复定义相同变量数组
For循环,For循环里面是父级做用域 for(let i=0;i<3;i++){ let i=’abc’; console.log(i) //打印出3次‘abc’(i在不一样得做用域) }
const 定义常量 特性和let同样
(1)const定义的定义必须有值,不能后赋值,不能修改
若是真的想将对象冻结,应该使用Object.freeze方法。浏览器
const foo = Object.freeze({}); // 常规模式时,下面一行不起做用; // 严格模式时,该行会报错 foo.prop = 123;
Let [a,b,c]=[2,3,4] 注意:左右俩边,结构格式要保持一致 Json: var obj={ a:2, b:3 }; let {a,b}=obj; let {a:nsds,b}=obj;//将a起名字为nsds let [a,b,c='暂无数据']=['aa','bb']; //能够设置默认值
交换俩个数的位置数据结构
let a=9; let b=8; [a,b]=[b,a];
案例app
function show({a="1",b='2'}){ console.log(a,b) //给默认值 } show({});
注意:ES6 内部使用严格相等运算符(===),判断一个位置是否有值。因此,只有当一个数组成员严格等于undefined,默认值才会生效。函数
let [x = 1] = [undefined]; x // 1 let [x = 1] = [null]; x // null
(1)优势:随意换行this
格式:${name}
code
let name='abc'; let age=18; let str=`这我的名字:${name},年龄:${age}`; console.log(str)
(2)字符串查找对象
str.indexOf(要找的东西); //返回索引(位置),没有找到返回-1
str.includes(要找的东西); //返回值 true/false
判断浏览器:navigator.userAgent.includes('chrome')
检测字符串以谁开头:str.startsWith(检测东西)
检测字符串以谁结尾:str.endsWith(检测东西)
重复字符串:str.repeat(3) 重复3次
字符串填充:str.padStart(整个字符串长度,填充东西) //往前填充
str.padEnd(str.length+str1.Length,填充东西) //日后填充索引
1.函数默认参数
function show({a="1",b='2'}={}){ console.log(a,b) //函数的特性 } show()
2.函数参数默认已经定义,不能再使用let,const声明:
function show(a=18){ Let a=12; //错误 console.log(a) } show()
3.扩展运算符,reset运算符... (展开/收起 数组)
let arr=['apple','banana','orage'] console.log(...arr) //apple banana orage function show(a,b,...c){ console.log(a,b)//1 2 console.log(c)//[3,4,5,6] } show(1,2,3,4,5,6)
4.箭头函数=>
Let show()=>a // 至关于return a
A.this问题,定义函数所在的对象,不在运行时所在的对象
B.箭头函数里没有arguments,用‘...’
C.箭头函数不能用于构造函数
1.Arr.forEach()//代替普通for
Arr.forEach(循环回调函数,this指向谁)
let arr=[1,2,3,4,5,6]; arr.forEach(function(val,index,arr){ console.log(this) // this指123 },123)
2.Arr.map()
正常状况下配合return,返回一个新的数组。若没有return至关于forEach()
从新整理数据结构:
3.Arr.filter() :
过滤,过滤一贯不合适“元素”,若是回调函数返回的时true,则留下 [{title:’aaa’}]->[{t:’aaa’}]
4.Arr.some()
相似查找,数组里面某一个元素符合条件,返回true;
5.Arr.every()
数组里面全部的元素都要符合条件,才返回true
6.Arr.reduce() 2**3求幂
从左往右求数组的和,阶层
7.Arr.reduceRight() //从右往左
for...of循环
默认循环的是value
arr.key() 数组下标
Arr.entries() 数组的某一项
Array.from()
做用:把类数组(获取一组元素,arguments...)对象转成数组
只要有length就靠谱
Array.of()
把一组值,转成数组
Array.find()
查找,找出第一个符合条件的数组成员,若是没有找到,返回undefined
Array.findIndex()
找的是位置,没找到返回-1
Array.fill()填充
Array.fill(填充的东西 开始位置,结束位置)