es6常用功能

1、const和let

在ES6之前,我们都是用var关键字声明变量。无论声明在何处,都会被视为声明在函数的最顶部(不在函数内即在全局作用域的最顶部)。这就是函数变量提升例如:

function aa() {
    if(flag) {
        var test = 'hello man'
    } else {
        console.log(test)
    }
  }

实际上是

function aa() {
    var test // 变量提升,函数最顶部
    if(flag) {
        test = 'hello man'
    } else {
        //此处访问 test 值为 undefined
        console.log(test)
    }
    //此处访问 test 值为 undefined
  }

而let和const都是块级作用域(一个花括号内可认为块级作用域,如函数或代码块内)

const声明必须赋值所以一般用于常量,无法被修改

2、字符串

模板字符串 使用``来定义其中可以加上${}拼接赋值

用途一:快速赋值

//ES5 
    var name = 'lux'
    console.log('hello' + name)
    //es6
    const name = 'lux'
    console.log(`hello ${name}`) //hello lux

用途二:多行字符串

// ES5
    var msg = "Hi \
    man!
    "
    // ES6
    const template = `<div>
        <span>hello world</span>
    </div>`

常用字符串处理方法:

1.includes:判断是否包含然后直接返回布尔值
2.repeat: 获取字符串重复n次
3. startsWith 和 endsWith 判断是否以 给定文本 开始或者结束
4. padStart 和 padEnd 填充字符串,应用场景:时分秒

 3、函数

默认传参

function action(num = 200) {
        console.log(num)
    }
    action(0) // 0
    action() //200
    action(300) //300

箭头函数

传参大于等于两个时用括号包裹,this可继承

//例如:
    [1,2,3].map(x => x + 1)
    
//等同于:
    [1,2,3].map((function(x){
        return x + 1
    }).bind(this))

 4、解构以及展开运算符

解构

//对象
    const people = {
        name: 'lux',
        age: 20
    }
    const { name, age } = people
    console.log(`${name} --- ${age}`)
    //数组
    const color = ['red', 'blue']
    const [first, second] = color
    console.log(first) //'red'
    console.log(second) //'blue'

展开运算符...

//数组
    const number = [1,2,3,4,5]
    const [first, ...rest] = number
    console.log(rest) //2,3,4,5
    //对象
    const user = {
        username: 'lux',
        gender: 'female',
        age: 19,
        address: 'peking'
    }
    const { username, ...rest } = user
    console.log(rest) //{"address": "peking", "age": 19, "gender": "female"
}

5.import 和 export

6、promise

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值