ES6 笔记总结

1.变量

  • var 可以重复声明,无法限制修改,函数级(没有块级作用域)
  • let 不能重复声明,块级作用域,变量 - 可以修改
  • const 不能重复声明,块级作用域,常量 - 不能修改

2.箭头函数

  • 1.方便
    • 如果只有一个参数,()可以省略
    • 如果只有一个 return ,{}可以省略
  • 修正this

3.参数扩展

  • 1.收集剩余的参数

  • function show(a, b, ...args){
        剩余的参数必须是最后一个
    }
    
  • 展开数组

  • 展开后的效果,跟直接打印数组内容是一样的

  • let arr1 = [1,2,3]
    let arr2 = [5,6,7]
    
    let arr = [...arr1, ...arr2]
    alert(arr);
    
  • function show(...args){
        fn(...args)
    }
    
    function fn(a,b){
        alert(a+b)
    }
    
    show(12,5)
    

4.数组方法

  • map 映射
  • [12,58,99]

  • [不及格,不及格,及格]

    let arr=[12,5,8];
    
    let result = arr.map(function(item){
        return item*2;
    })
    alert(result)
    
    
    let score = [19,85,99,25,90];
    let result = score.map(item => item>=60?'及格':'不及格')
    alert(score);
    alert(result);
    
  • reduce 汇总: 一堆 -> 一个

    reducer 函数接收4个参数:
    
    Accumulator (acc) (累计器)
    Current Value (cur) (当前值)
    Current Index (idx) (当前索引)
    Source Array (src) (源数组)
    有默认参数
    const arr = [1, 2, 3]
    let result = arr.reduce(function(val, item, index, origin) {
      return val + item
    }, 0)
    1
    2
    3
    4
    无默认参数
    result = arr.reduce(function(val, item, index, origin) {
      return val + item
    })
    1
    2
    3
    求平均值
    result = arr.reduce(function(val, item, index, origin) {
      const res = val + item
      if (index === origin.length-1) {
        return res/origin.length
      } else {
        return res
      }
    })
    console.log(result)
    
    
  • filter 过滤:一堆 -> 剩下的

    let arr = [12,5,8,99]
    let result = arr.filter(item =>{
        if(item%3==0){
            return true;
        }else{
            return false;
        }
    });
    
    
    let arr = [
        {	title:'衬衫',prise:75	},
        {	title:'女式包',prise:7500 }
    ];
    
    let result = arr.filter(json => json.price>=1000);
    	console.log(result);
    
  • forEach 循环

    let arr = [12,5,8,9];
    
    arr.forEach((item,index) => {
        alert(index+':'+item)
    });
    

5.字符串

  • startsWith/endsWith
  • 字符串模板: ${a}xxx${b}

6.promise

  • 封装异步操作
  • promise.all([])

7.generator

function *show(){

​ yield

}

8.Json

  • Json.stringfy({a:12,b:5}) 转化成 ‘{“a”:12,“b”:5}’

  • Json.stringfy(‘{“a”:12,“b”:5}’) 转化成 {a:12,b:5}

  • 注意:

    1. 名字跟值一样(key和value),可以简写为一个

      1. 方法: :function 一块删除
      show: function(){}
      show(){}
      
  • json的标准写法:

  • 1.只能用双引号

  • 2.所有的名字都必须用引号包起来

    { a:12, b:5}{ "a":12, "b":5}{ a:'abc',b:5}{ "a":"abc", "b":5}

9.解构赋值

  • let [a,b,c] = [12,5,8];
    
    let {a,b,c} = {a:12,b:5,c:8}
    
  • 注意:

  • 1.左右结构一样

  • 右边是合法的形式

  • 声明,赋值一次完成(必须在一句话里完成)

10.面向对象

class Test{
    constructor(){
        this.xxx=
    }
        方法一(){
            
        }
        方法二(){
            
        }
}
    
    
    class CLs2 extends CLs1{
         constructor(){
             super();
         }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值