简介
为什么使用ES6?
新增语法
let
if (true) {
let a = 10;
console.log(a); // 10
}
console.log(a); // Uncaught ReferenceError: a is not defined
for (var j = 0; j < 2; j++) {}
console.log(j) // 2
for (let i = 0; i < 2; i++) {}
console.log(i);//Uncaught ReferenceError: i is not defined
let
命令,它所声明的变量就“绑定”这个区域,不再受外部的影响。 var num = 10;
if (true) {
console.log(num);
//Uncaught ReferenceError: Cannot access 'num' before initialization
let num = 20;
}
var arr = [];
for (var i = 0; i < 2; i++) {
arr[i] = function(){
console.log(i);
}
}
arr[0]();
arr[1]();
解析:var声明变量为全局变量,循环结束时i = 2,所以两次打印都为2
var arr = [];
for (let i = 0; i < 2; i++) {
arr[i] = function(){
console.log(i);
}
}
arr[0]();
arr[1]();
解析:每次循环都会产生一个块级作用域,每个块级作用域中的变量都是不同的,函数执行时输出的是自己上一级(循环产生的块级作用域)作用域下的值。所以依次打印出 0 和 1。
const
const PI; //Uncaught SyntaxError: Missing initializer in const declaration
const PI = 3.14;
PI = 100; //Uncaught TypeError: Assignment to constant variable.
const ary = ['100','200'];
ary[0] = 101;
ary[1] = 102;
console.log(ary);// [101, 102]
ary = ['101','102'];//Uncaught TypeError: Assignment to constant variable.
let const var 的区别
当声明不变化的(如函数)使用const,从而让JavaScript不必一直监测该值的变化,提升效率。
解构赋值
数组解构 允许我们按照一一对应的关系从数组中提取值,然后将值赋值给变量。如果解构不成功,变量的值为undefined
let [a,b,c,d,e] = [1,2,3];
console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
console.log(d); // undefined
console.log(e); // undefined
对象解构 允许我们使用变量的名字匹配对象的属性 匹配成功将对象属性的值赋值给变量,psex为别名使用
let person = {name : 'zhangsan',age : 30,sex : 'nan'};
let { name, page, sex : psex } = person;
console.log(name); // zhangsan
console.log(page); // undefined
console.log(psex); // nan
箭头函数
ES6中新增的定义函数的方式,用来简化函数定义语法的
const fn = () => {
console.log(1);
}
const sum = (num1,num2) => console.log(num1 + num2) ;
const fn = v => console.log(v);
function fn(){
console.log(this);
return () =>{
console.log(this);
}
}
const obj = {name: 'zhangsan '};
fn.call(obj);
//{name: "zhangsan "}
const resFn = fn.call(obj);
resFn();
/*
{name: "zhangsan "}
{name: "zhangsan "}
*/
var obj = {
age : 20,
say : () =>{
alert(this.age);
}
}
obj.say(); //undefined
剩余参数
剩余参数语法允许我们将一个不定数量的参数表示为一个数组
const sum = (...args) => {
let total = 0;
args.forEach( item => total += item );
return total;
}
console.log(sum(10,20,30));
剩余参数和解构配合使用
let students = ['zhou','wu','sheng'];
let [s1,...s2] = students;
console.log(s1);//zhou
console.log(s2);//["wu", "sheng"]
Array的扩展方法-扩展运算符
扩展运算符(展开语法)
let ary = [1,2,3];
console.log(...ary);//1 2 3
let ary1 = [1,2,3];
let ary2 = [4,5];
//方法一
let ary3 = [...ary1,...ary2];
//方法二
ary1.push(...ary2);
var oDivs = document.getElementsByTagName('div');
var ary = [...oDivs];
var ary2 = Array.from(oDivs);
let arrayLike = {
'0':1,
'1':2,
'length':2
}
let newAry = Array.from(arrayLike,item => item *2);
String的扩展方法 - 模板字符串
let name = `模板字符串`;
let other = `nihao ${name}`;
let html = `
${name}
${other}
`;
const fun = () => 'fanhuizhi';
let greet = `${fun()}`;
startsWith() 表示参数字符串是否在原字符串的头部,返回布尔值
endsWith() 表示参数字符串是否在原字符串的尾部,返回布尔值
let str = 'hello world';
str.startsWith('hell');
console.log(str.startsWith('hell'));//true
str.endsWith('d');
console.log(str.endsWith('d'));//true
'x'.repeat(2);
console.log('x'.repeat(2));//xx
set数据结构
const s1 = new Set();
console.log(s1.size);//0
const s2 = new Set(['a','b']);
console.log(s2.size);//2
const s3 = new Set(['a','b','b','a']);
console.log(s3.size);//2
const ary = [...s3];
console.log(ary);//["a", "b"]
const s4 = new Set([1,2,3,4,5]);
s4.forEach(val=>{
console.log(val);
})