前端知识点——ES6(ECMA 2015)

写在前面

笔记内容大多出自于拉勾教育大前端高薪训练营的教程,因此也许会和其他文章雷同较多,请担待。

let const

  • 没有变量提升
  • const常量,readonly,索引不变的前提下可以修改内部值
for(let i = 0; i < 3; i++) {
   
  for(let i = 0; i < 3; i++) {
   
    //这里虽然有两次定义i,但是因为是i,所以作用域不同,不冲突。
    console.log(i) // 0 1 2  0 1 2  0 1 2
  }
}

解构

数组的解构

const [a, b, c = 'C'] = ['A', 'B']
console.log(a, b, c) // "A" "B" "C"

const [a, b, c = 'C'] = ['A', 'B', 3]
console.log(a, b, c) // "A" "B" 3

对象的解构

const obj = {
   
  name: 'aeorus',
  age: 27
}
const {
   
  name: userName, // 重命名
  age
} = obj
console.log(userName, age) // "aeorus" 27

模板字符串

  • 支持换行
  • 插值表达式 -> ${}
  • 可以使用标签 -> 用途类似拦截器
const str = console.log`aeorus` // ["aeorus", raw: ["aeorus"]]
// 自定义标签
const name = 'aeorus'
const age = 27
function myTagFunc(strings, name, age) {
   
  console.log(strings) // ["My name is ", " and I'm ", " years now.", raw: ["My name is ", " and I'm ", " years now."]]
  console.log(name) // "aeorus"
  console.log(age) // 27
  return 'rebuild result'
}
const str = myTagFunc`My name is ${
     name} and I'm ${
     age} years now.`
console.log(str) // "rebuild result"

字符串的扩展方法

  • includes()
  • startsWith()
  • endsWith()
const name = 'aeorus'
console.log(name.includes('A')) // false
console.log(name.startsWith('a')) // true
console.log(name.endsWith('s')) // true

对象字面量增强

const name = 'aeorus'
const obj = {
   
  // name: name
  name,
  // method1: function() {}
  method1() {
   },
  // 可以使用[key]方式添加动态key的键值对
  [Math.random()]: 'random'
}

Object.assign

Object.assign(target, source) -> target和source的交集,同名的key以source为准

Proxy

Proxy <-> Object.defineProperty 数据劫持

const obj = {
   
  name: 'aeorus',
  age: 27
}
// Object.defineProperty
for (let key in obj) {
   
  Object.defineProperty(obj, key, {
   
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值