ES6必须掌握的知识点(一)
- let、 const
- 解构赋值
- 字符串
let、 const
let 相当于之前的var、const 常量 定义好了不能改变
<!-- let 使用注意 -->
<!--1、没有预解析,不存在变量提升 在代码块内,只有let定义变量,在之前使用,都是报错,先定义完,在使用-->
let a = 12;
function fn(){
alert(a);
let a = 5
}
fn();
<!--失败:Cannot access 'a' before initialization-->
<!--2、同一个作用域里,不能重复定义变量-->
let str
let str
<!--const 使用注意-->
<!--1、const定义变量,不能修改-->
<!--2、const定义完变量,必须有值-->
解构赋值 ⭐⭐⭐
左右两边,结构格式得保持一致
<!--解构数组-->
let [a,b,c] = [12,2,6]
console.log(a,b,c)
<!--解构对象-->
let json = {name:'友人',age:18}
let {name,age} = json
console.log(name,age)
<!--定义别名-->
let {name:n,age:a} = json
console.log(n,a)
<!--解构时可以给默认值-->
let [a,b,c="暂无数据"] = [2,5]
console.log(a,b,c)
<!--交换赋值-->
let a = 12;
let b = 5;
[a,b] = [b,a]
console.log(a,b)
字符串
<!--字符串模板-->
let name = '友人'
let age = 18
let str = `我叫${name},今年${age};`
console.log(str)
<!-- 字符串查找 -->
let str = 'apple , banana';
console.log(str.indexOf('banana') > -1)//indexOf返回下标
console.log(str.includes('banana')) //includes找到返回true,否则false
<!-- 字符串是否以谁开头 -->
let url1 = 'ftp://D:/'
let url2 = 'http://www.baidu.com'
console.log(url1.startsWith('http')) //返回false
console.log(url2.startsWith('http')) //返回true
<!-- 字符串是否以谁结尾 -->
console.log(url1.endsWith('com')) //返回false
console.log(url2.endsWith('com')) //返回true