ES6语法

ES6语法

let 和 const 关键字

我们以前都是使用 var 关键字来声明变量的

在 ES6 的时候,多了两个关键字 let 和 const ,也是用来声明变量的

只不过和 var 有一些区别

1. let 和 const 不允许重复声明变量

// 使用 var 的时候重复声明变量是没问题的,只不过就是后的会把前面覆盖掉

var num = 100
var num = 200
2、let和const声明的变量不会进行声明提升。(通过let声明变量,之前的区域,叫做暂时性死区)
3、let和const声明的变量会被所有的代码块限制作用域(作用域更小),只要遇到大括号就形成作用域。

 let num;
        alert(num); //undefined
         const num2 = 20;
         alert(num2);

// 使用const 重复声明变量的时候就会报错

const num = 100 
const num = 200 // 这里就会报错了

ii. let 和 const 声明的变量不会在预解析的时候解析(也就是没有变量提 升)

alert(num);
         const num = 10;
         alert(num);`

iii. let 和 const 声明的变量会被所有代码块限制作用范围

// var 声明的变量只有函数能限制其作用域,其他的不能限制

if (true) {
var num = 100 
}
console.log(num) // 100

// let 声明的变量,除了函数可以限制,所有的代码块都可以限制其作用域 (if/while/for/…)

if (true) {
let num = 100 console.log(num) // 100
}
console.log(num) // 报错
let 和 const 的区别
let num num = 100 console.log(num) // 100

const num // 这里就会报错了,因为 const 声明的时候必须赋值
箭头函数

箭头函数是 ES6 里面一个简写函数的语法方式

重点: 箭头函数只能简写函数表达式,不能简写声明式函数

function fn() {} // 不能简写 const fun = function () {} // 可以简写 const obj = { fn: function () {} // 可以简写 }

语法: (函数的行参) => { 函数体内要执行的代码 }

function fn() {} // 不能简写
const fun = function () {} // 可以简写
const obj = { fn: function () {} // 可以简写 }

语法: (函数的行参) => { 函数体内要执行的代码 }

var add = (x, y) => x + y;
            alert(add(30, 40));

// 可以使⽤箭头函数写成 const obj2 = { fn: (a, b) => { console.log(a) console.log(b) } }

箭头函数的特殊性

箭头函数内部没有 this,箭头函数的 this 是上下文的 this

// 在箭头函数定义的位置往上数,这一行是可以打印出 this 的 // 因为这里的 this 是 window // 所以箭头函数内部的 this 就是 window

const obj = {
fn: function () { console.log(this)
},
// 这个位置是箭头函数的上一行,但是不能打印出 this
fun: () => {
// 箭头函数内部的 this 是书写箭头函数的上一行一个可以打印出 this 的位 置

console.log(this)
}
}
obj.fn()
obj.fun()

按照我们之前的 this 指向来判断,两个都应该指向 obj

  • 但是 fun 因为是箭头函数,所以 this 不指向 obj,而是指向 fun 的外层, 就是 window
箭头函数内部没有 arguments 这个参数集合
const obj = { fn: function () { console.log(arguments) }, fun: () => { console.log(arguments) } } obj.fn(1, 2, 3) // 会打印⼀个伪数组 [1, 2, 3] obj.fun(1, 2, 3) // 会直接报错
函数的行参只有一个的时候可以不写 () 其余情况必须写

const obj = { 
fn: () => { 
console.log('没有参数,必须写小括号') 
}, 
n2: a => { 
console.log('一个行参,可以不写小括号') }, 
fn3: (a, b) => { console.log('两个或两个以上参数,必须写小括号') 
} 
}
函数体只有一行代码的时候,可以不写 {} ,并且会自动 return

const obj = {
fn: a => {
return a + 10
},
fun: a => a + 10
}
console.log(fn(10)) // 20 console.log(fun(10)) // 20

###### 函数传递参数的时候的默认值

当我不传递参数的时候,使用默认值,传递参数了就使用传递的参数

function fn(a) {
a = a || 10
console.log(a)
}
fn() // 不传递参数的时候,函数内部的 a 就是 10
fn(20) // 传递了参数 20 的时候,函数内部的 a 就是 20

###### 在 ES6 中我们可以直接把默认值写在函数的行参位置

function fn(a = 10) { console.log(a)
} 
fn() // 不传递参数的时候,函数内部的 a 就是 10
fn(20) // 传递了参数 20 的时候,函数内部的 a 就是 20

###### 这个默认值的方式箭头函数也可以使用

const fn = (a = 10) => { console.log(a) } 

fn() // 不传递参数的时候,函数内部的 a 就是 10

fn(20) // 传递了参数 20 的时候,函数内部的 a 就是 20

** 注意: 箭头函数如果你需要使用默认值的话,那么一个参数的时候也需要 写 ()
**

##### 解构赋值

解构赋值,就是快速的从对象或者数组中取出成员的一个语法方式

###### 解构对象
快速的从对象中获取成员

// ES5 的方法向得到对象中的成员

const obj = { 
name: 'Jack', 
age: 18, 
gender: '男' 
}

let name = obj.name 
let age = obj.age 
let gender = obj.gender

// 解构赋值的方式从对象中获取成员
const obj = {
name: ‘Jack’,
age: 18, gender: ‘男’
}


// 前面的 {} 表示我要从 obj 这个对象中获取成员了
// name age gender 都得是 obj 中有的成员
// obj 必须是一个对象
let { name, age, gender } = obj

解构数组
快速的从数组中获取成员
// ES5 的方式从数组中获取成员 
const arr = ['Jack', 'Rose', 'Tom'] 
let a = arr[0] 
et b = arr[1] 
let c = arr[2]

// 使用解构赋值的⽅式从数组中获取成员 const arr = [‘Jack’, ‘Rose’, ‘Tom’]
// 前面的 [] 表示要从 arr 这个数组中获取成员了
// a b c 分别对应这数组中的索引 0 1 2
// arr 必须是一个数组
let [a, b, c] = arr

**注意 {} 是专门解构对象使用的
[] 是专门解构数组使用的

不能混淆

模版字符串

ES5 中我们表示字符串的时候使用 ‘’ 或者 “”

在 ES6 中,我们还有一个东西可以表示字符串,就是 ``(反引号)

let str = hello world console.log(typeof str) // string

和单引号好友双引号的区别

i. 反引号可以换行书写

// 这个单引号或者双引号不能换行,换行就会报错了

let str = ‘hello world’ // 下面这个就报错了
let str2 = ‘hello world’
let str = hello world console.log(str) // 是可以使用的

反引号可以直接在字符串里面拼接变量
// ES5 需要字符串拼接变量的时候 
let num = 100 
let str = 'hello' + num + 'world' + num console.log(str) // hello100world100

// 直接写在字符串里面不好使 let str2 = 'hellonumworldnum' console.log(str2) // hellonumworldnum

ii. 反引号可以直接在字符串里面拼接变量

// ES5 需要字符串拼接变量的时候 let num = 100 let str = 'hello' + num + 'world' + num console.log(str) // hello100world100

// 直接写在字符串里面不好使 let str2 = 'hellonumworldnum' console.log(str2) // hellonumworldnum

// 模版字符串拼接变量 let num = 100 let str = `hello${num}world${num}` console.log(str) // hello100world100

在 `` 里面的 ${} 就是⽤来书写变量的位置

展开运算符

ES6 里面号新添加了一个运算符 … ,叫做展开运算符

作用是把数组展开

let arr = [1, 2, 3, 4, 5] console.log(...arr) // 1 2 3 4 5

合并数组的时候可以使用

let arr = [1, 2, 3, 4] 
let arr2 = [...arr, 5] console.log(arr2)

也可以合并对象使用

let obj = { 
name: 'Jack', 
age: 18 
}
let obj2 = { 
...obj, 
gender: '男' 
}
console.log(obj2)

在函数传递参数的时候也可以使用

let arr = [1, 2, 3]
function fn(a, b, c) { console.log(a) 
console.log(b) 
console.log(c) } 
fn(...arr) 
// 等价于 fn(1, 2, 3)

ES6语法今天就跟大家介绍到这里,喜欢的朋友可以关注一下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值