0.什么是ES6

ES 的全称是 ECMAScript , 它是由 ECMA 国际标准化组织,制定的一项脚本语言的标准化规范。

ES6 实际上是一个泛指,泛指 ES2015 及后续的版本。

1.let、const、var的区别

let是es6中新增的语法

let只对当前区块定义有效:

if(1){
let a=10;
console.log(a) //输出10
}
console.log(a) // a is not defined
if(1){ let a=10; console.log(a) //输出10 } console.log(a) // a is not defined
   if(1){
      let a=10;
      console.log(a) //输出10
    }
    console.log(a)  // a is not defined
let a=20;
if(true){
let a=10;
console.log(a) //输出10
}
console.log(a) //输出20
let a=20; if(true){ let a=10; console.log(a) //输出10 } console.log(a) //输出20
    let a=20;
    if(true){
      let a=10;
      console.log(a) //输出10
    }
    console.log(a)  //输出20

不存在变量提升:

if(true){
let a=10;
console.log(a) //输出10
}
console.log(a) //a is not defined
let a=20;
if(true){ let a=10; console.log(a) //输出10 } console.log(a) //a is not defined let a=20;
 if(true){
      let a=10;
      console.log(a) //输出10
    }
    console.log(a)  //a is not defined
    let a=20;

var则更强

if(true){
var a=10;
console.log(a) //输出10
}
console.log(a) //输出10
if(true){ var a=10; console.log(a) //输出10 } console.log(a) //输出10
   if(true){
      var a=10;
      console.log(a) //输出10
    }
    console.log(a)  //输出10
var a=20;
console.log(a) //输出20
if(true){
var a=10;
console.log(a) //输出10
}
console.log(a) //输出10
var a=20; console.log(a) //输出20 if(true){ var a=10; console.log(a) //输出10 } console.log(a) //输出10
    var a=20;
    console.log(a) //输出20
    if(true){
      var a=10;
      console.log(a) //输出10
    }
    console.log(a)  //输出10
console.log(a) //undefined
if(true){
console.log(a) //undefined
}
console.log(a) //undefined
var a=10
console.log(a) //undefined if(true){ console.log(a) //undefined } console.log(a) //undefined var a=10
   console.log(a) //undefined
    if(true){
      console.log(a) //undefined
    }
    console.log(a)  //undefined
    var a=10

const是常量,必须赋值,且赋值后不能修改。

总结:

varletconst
函数级作用块级作用域块级作用域
变量提升不存在变量提升不存在变量提升
值可以修改值可以修改值不可以更改
二、解构赋值

数组解构

let [a, b] = [1, 2];
console.log(a) //输出1
console.log(b) //输出2
let = []; //c undefined
console.log(c) //输出undefined
let [a, b] = [1, 2]; console.log(a) //输出1 console.log(b) //输出2 let = []; //c undefined console.log(c) //输出undefined
    let [a, b] = [1, 2];
    console.log(a)  //输出1
    console.log(b)	//输出2

    let  = [];  //c undefined
    console.log(c)  //输出undefined

对象解构

let person = { name: 'zhangsa', age: 18 };
let { name, age } = person;
console.log(name); // 输出'zhangsa'
console.log(age); // 输出 18
let {name: myName, age: myAge} = person; // myName myAge 属于别名
console.log(myName); // 输出'zhangsa'
console.log(myAge); // 输出 18
let person = { name: 'zhangsa', age: 18 }; let { name, age } = person; console.log(name); // 输出'zhangsa' console.log(age); // 输出 18 let {name: myName, age: myAge} = person; // myName myAge 属于别名 console.log(myName); // 输出'zhangsa' console.log(myAge); // 输出 18
   let person = { name: 'zhangsa', age: 18 }; 
    let { name, age } = person;
    console.log(name); // 输出'zhangsa' 
    console.log(age); // 输出 18
   
    let {name: myName, age: myAge} = person; // myName myAge 属于别名
    console.log(myName); // 输出'zhangsa' 
    console.log(myAge); // 输出 18
   
三、箭头函数

格式:

() => {}
const fn = () => {}
() => {} const fn = () => {}
() => {} 
const fn = () => {}

函数体中只有一句代码,且代码的执行结果就是返回值,可以省略大括号

function sum(num1, num2) {
return num1 + num2;
}
const sum = (num1, num2) => num1 + num2;
function sum(num1, num2) { return num1 + num2; } const sum = (num1, num2) => num1 + num2;
function sum(num1, num2) { 
     return num1 + num2; 
 }
 const sum = (num1, num2) => num1 + num2; 

如果形参只有一个,可以省略小括号

function fn (x) {
return x;
}
const fn = x=> x;
function fn (x) { return x; } const fn = x=> x;
function fn (x) {
     return x;
 } 
 const fn = x=> x;

箭头函数不绑定this关键字,箭头函数中的this,指向的是函数定义位置的上下文this。

const obj = { name: '张三' }
function fn() {
console.log(this); // {name: '张三'}
return () => {
console.log(this) // {name: '张三'}
}
}
const resFn = fn.call(obj);
resFn();
const obj = { name: '张三' } function fn() { console.log(this); // {name: '张三'} return () => { console.log(this) // {name: '张三'} } } const resFn = fn.call(obj); resFn();
    const obj = { name: '张三' }
    function fn() {
      console.log(this); // {name: '张三'}
      return () => {
        console.log(this)  // {name: '张三'}
      }
    }
    const resFn = fn.call(obj);
    resFn();
四、剩余参数

剩余参数语法允许我们将一个不定数量的参数表示为一个数组

function sum (first, ...args) {
console.log(first); // 1
console.log(args); // [2, 3,4]
}
sum(1, 2, 3,4)
function sum (first, ...args) { console.log(first); // 1 console.log(args); // [2, 3,4] } sum(1, 2, 3,4)
    function sum (first, ...args) {
      console.log(first); // 1
      console.log(args); // [2, 3,4] 
  }
  sum(1, 2, 3,4)

剩余参数和解构配合使用

let students = ['wangwu', 'zhangsan', 'lisi'];
let [s1, ...s2] = students;
console.log(s1); // 'wangwu'
console.log(s2); // ['zhangsan', 'lisi']
let students = ['wangwu', 'zhangsan', 'lisi']; let [s1, ...s2] = students; console.log(s1); // 'wangwu' console.log(s2); // ['zhangsan', 'lisi']
 let students = ['wangwu', 'zhangsan', 'lisi'];
    let [s1, ...s2] = students; 
    console.log(s1);  // 'wangwu' 
    console.log(s2);  // ['zhangsan', 'lisi']
五、ES6 的内置对象扩展——Array 的扩展方法

1.合并数组

let arr1 = ["1", "2"];
let arr2 = ["3", "4"];
let arr = [...arr1, ...arr2];
console.log(arr); //['1', '2', '3', '4']
let arr1 = ["1", "2"]; let arr2 = ["3", "4"]; let arr = [...arr1, ...arr2]; console.log(arr); //['1', '2', '3', '4']
 let arr1 = ["1", "2"];
        let arr2 = ["3", "4"];
        let arr = [...arr1, ...arr2];
        console.log(arr); //['1', '2', '3', '4']

2.数组克隆

let arr1 = ["1", "2"];
let arr2 = ["3", "4"];
let arr = [...arr1, ...arr2];
console.log(arr); // ['1', '2', '3', '4']
let arr3 = [...arr2];
console.log(arr3); // ['3', '4']
let arr1 = ["1", "2"]; let arr2 = ["3", "4"]; let arr = [...arr1, ...arr2]; console.log(arr); // ['1', '2', '3', '4'] let arr3 = [...arr2]; console.log(arr3); // ['3', '4']
        let arr1 = ["1", "2"];
        let arr2 = ["3", "4"];
        let arr = [...arr1, ...arr2];
        console.log(arr); //  ['1', '2', '3', '4']
        let arr3 = [...arr2];
        console.log(arr3); // ['3', '4']

3.将类数组或可遍历对象转换为真正的数组

let oDivs = document.getElementsByTagName('div');
oDivs = [...oDivs];
let oDivs = document.getElementsByTagName('div'); oDivs = [...oDivs];
let oDivs = document.getElementsByTagName('div'); 
oDivs = [...oDivs];

4.Array.from()方法

可以将伪数组转换成真正的数组

let s="12345"
let arr= Array.from(s) // [1,2,3,4,5]
console.log(arr) //['1', '2', '3', '4', '5']
let s="12345" let arr= Array.from(s) // [1,2,3,4,5] console.log(arr) //['1', '2', '3', '4', '5']
   let s="12345" 
   let arr= Array.from(s) // [1,2,3,4,5]
   console.log(arr) //['1', '2', '3', '4', '5']

将类数组转换成数组

//
let arr1 = {
1: 'a',
2: 'b',
'length': 3
}
console.log(Array.from(arr1));//undefined ,a,b.
//也可以接受第二个参数,用于对每个参数单独处理一次
let arr2 = {
1: 2,
2: 12,
'length': 3
}
let newAry = Array.from(arr2, item => item*3)
console.log(newAry) // [NaN, 6, 36]
// let arr1 = { 1: 'a', 2: 'b', 'length': 3 } console.log(Array.from(arr1));//undefined ,a,b. //也可以接受第二个参数,用于对每个参数单独处理一次 let arr2 = { 1: 2, 2: 12, 'length': 3 } let newAry = Array.from(arr2, item => item*3) console.log(newAry) // [NaN, 6, 36]
  //
    let arr1 = {
      1: 'a',
      2: 'b',
      'length': 3
    }
    console.log(Array.from(arr1));//undefined ,a,b.
    //也可以接受第二个参数,用于对每个参数单独处理一次
    let arr2 = {
      1: 2,
      2: 12,
      'length': 3
    }
    let newAry = Array.from(arr2, item => item*3)
    console.log(newAry) // [NaN, 6, 36]

5.find()方法

用于找出第一个符合条件的数组成员,如果没有找到返回undefined

let arr1 = [1,2,3,2];
let target = arr1.find( item => item==2);
console.log(target);//2,如果未找到,返回undefined
let person = [
{name:"张三",age:16},
{name:"李四",age:17},
{name:"王五",age:18},
]
let target2 = person.find((item,index)=>{return item.name=='张三'});
console.log(target2.name); //张三
let arr1 = [1,2,3,2]; let target = arr1.find( item => item==2); console.log(target);//2,如果未找到,返回undefined let person = [ {name:"张三",age:16}, {name:"李四",age:17}, {name:"王五",age:18}, ] let target2 = person.find((item,index)=>{return item.name=='张三'}); console.log(target2.name); //张三
    let arr1 = [1,2,3,2];
    let target = arr1.find( item => item==2);
    console.log(target);//2,如果未找到,返回undefined
    let person = [
      {name:"张三",age:16},
      {name:"李四",age:17},
      {name:"王五",age:18},
    ]
    let target2 = person.find((item,index)=>{return item.name=='张三'});
    console.log(target2.name); //张三

6.findindex()方法

用于找出第一个符合条件的数组成员的位置,如果没有找到返回-1

let ary = [1, 5, 10, 15];
let index = ary.findIndex((value, index) => value > 8 );
console.log("index=",index); // 2
let ary = [1, 5, 10, 15]; let index = ary.findIndex((value, index) => value > 8 ); console.log("index=",index); // 2
        let ary = [1, 5, 10, 15];
        let index = ary.findIndex((value, index) => value > 8 ); 
        console.log("index=",index); // 2

7.includes()方法

表示某个数组是否包含给定的值,返回布尔值。

let ary = [1, 5, 10, 15];
console.log(ary.includes(5)) //true
console.log(ary.includes(6)) //false
let ary = [1, 5, 10, 15]; console.log(ary.includes(5)) //true console.log(ary.includes(6)) //false
         let ary = [1, 5, 10, 15];
        console.log(ary.includes(5)) //true
        console.log(ary.includes(6)) //false
六、String 的扩展方法

1.模板字符

// 1.定义
let a=`hello`
console.log(a) //hello
// 2.解析变量
let name ='zhang'
let say=`hello, ${name}`
console.log(say) //hello,zhang
//3.字符串可以换行
let result = {
name: 'zhangsan',
age: 20, sex: '男'
}
let html = ` <div>
<span>${result.name}</span>
<span>${result.age}</span>
<span>${result.sex}</span>
</div> `;
//4.调用函数
const sayHello = function () {
return 'test';
};
let greet = `${sayHello()} 哈哈哈哈`;
console.log(greet); // 哈哈哈哈 test
// 1.定义 let a=`hello` console.log(a) //hello // 2.解析变量 let name ='zhang' let say=`hello, nameconsole.log(say)//hello,zhang//3.letresult=name:zhangsan,age:20,sex:lethtml=<div><span>{result.name}</span> <span>result.age</span><span>{result.sex}</span> </div> `; //4.调用函数 const sayHello = function () { return 'test'; }; let greet = `${sayHello()} 哈哈哈哈`; console.log(greet); // 哈哈哈哈 test
        // 1.定义
        let a=`hello`
        console.log(a) //hello

        // 2.解析变量
        let name ='zhang'
        let say=`hello, ${name}`
        console.log(say) //hello,zhang

        //3.字符串可以换行
        let result = { 
            name: 'zhangsan', 
            age: 20,      sex: '男' 
        } 
        let html = ` <div>
            <span>${result.name}</span>
            <span>${result.age}</span>
            <span>${result.sex}</span>
        </div> `;
        
        //4.调用函数
        const sayHello = function () { 
            return 'test';
         }; 
         let greet = `${sayHello()} 哈哈哈哈`;  
         console.log(greet); // 哈哈哈哈 test

2.String扩展方法

startsWith() 和 endsWith()

  • startsWith():表示参数字符串是否在原字符串的头部,返回布尔值
  • endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值
let str = 'Hello world!';
str.startsWith('Hello') // true
str.endsWith('!') // true
let str = 'Hello world!'; str.startsWith('Hello') // true str.endsWith('!') // true
let str = 'Hello world!';
 str.startsWith('Hello') // true 
 str.endsWith('!')       // true

3.repeat()

repeat方法表示将原字符串重复n次,返回一个新字符串。

'x'.repeat(3) // "xxx"
'hello'.repeat(2) // "hellohello"
'x'.repeat(3) // "xxx" 'hello'.repeat(2) // "hellohello"
'x'.repeat(3)      // "xxx" 
'hello'.repeat(2)  // "hellohello"
七、Set数据结构

ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。

Set本身是一个构造函数,用来生成 Set 数据结构。

const s = new Set();
const s = new Set();
const s = new Set();

Set函数可以接受一个数组作为参数,用来初始化。

const set = new Set([1, 2, 3, 4, 4]);
const set = new Set([1, 2, 3, 4, 4]);
const set = new Set([1, 2, 3, 4, 4]);

1.实例方法

  • add(value):添加某个值,返回 Set 结构本身
  • delete(value):删除某个值,返回一个布尔值,表示删除是否成功
  • has(value):返回一个布尔值,表示该值是否为 Set 的成员
  • clear():清除所有成员,没有返回值
const s = new Set();
s.add(1).add(2).add(3); // 向 set 结构中添加值
s.delete(2) // 删除 set 结构中的2值
s.has(1) // 表示 set 结构中是否有1这个值 返回布尔值
s.clear() // 清除 set 结构中的所有值
const s = new Set(); s.add(1).add(2).add(3); // 向 set 结构中添加值 s.delete(2) // 删除 set 结构中的2值 s.has(1) // 表示 set 结构中是否有1这个值 返回布尔值 s.clear() // 清除 set 结构中的所有值
const s = new Set();
 s.add(1).add(2).add(3); // 向 set 结构中添加值 
 s.delete(2)             // 删除 set 结构中的2值 
 s.has(1)                // 表示 set 结构中是否有1这个值 返回布尔值 
 s.clear()               // 清除 set 结构中的所有值

2.set遍历

Set 结构的实例与数组一样,也拥有forEach方法,用于对每个成员执行某种操作,没有返回值。

s.forEach(value => console.log(value))
s.forEach(value => console.log(value))
s.forEach(value => console.log(value))
分类: 前端