本文就为大家带来一篇基于es6作用域和解构赋值详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。
es6 强制开启严格模式
作用域
•var 声明局部变量,for/if花括号中定义的变量在花括号外也可访问
•let 声明的变量为块作用域,变量不可重复定义
•const 声明常量,块作用域,声明时必须赋值,不可修改
// const声明的k指向一个对象,k本身不可变,但对象可变
function test() {
const k={
a:1
}
k.b=3;
console.log(k);
}
test()解构赋值
{
let a, b, 3, rest;
[a, b, c=3]=[1, 2];
console.log(a, b);
}
//output: 1 2 3
{
let a, b, 3, rest;
[a, b, c]=[1, 2];
console.log(a, b);
}
//output: 1 2 undefined
{
let a, b, rest;
[a, b, ...rest] = [1, 2, 3, 4, 5, 6];
console.log(a, b, rest);
}
//output:1 2 [3, 4, 5, 6]
{
let a, b;
({a, b} = {a:1, b:2})
console.log(a ,b);
}
//output: 1 2
使用场景
变量交换
{
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b);
}
获取多个函数值
{
function f(){
return [1, 2]
}
let a, b;
[a, b] = f();
console.log(a, b);
}
获取多个函数返回值
{
function f(){
return [1, 2, 3, 4, 5]
}
let a, b, c;
[a,,,b] = f();
console.log(a, b);
}
//output: 1 4
{
function f(){
return [1, 2, 3, 4, 5]
}
let a, b, c;
[a, ...b] = f();
console.log(a, b);
}
//output: 1 [2, 3, 4, 5]
对象解构赋值
{
let o={p:42, q:true};
let {p, q, c=5} = o;
console.log(p ,q);
}
//output: 42 true 5
获取json值
{
let metadata={
title: 'abc',
test: [{
title: 'test',
desc: 'description'
}]
}
let {title:estitle, test:[{title:cntitle}]} = metadata;
console.log(estitle, cntitle);
}
//output: abc test
相关推荐:
实例详解es6中数组array新增方法
实例详解es6在react中的应用
详解es6中javascript实现函数绑定及类的事件绑定功能
以上就是详解基于es6作用域和解构赋值的详细内容。