码迷,mamicode.com
首页 > 其他好文 > 详细

es6--解构赋值-用途

时间:2017-12-18 01:19:02      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:模块   class   let   const   多个   return   key   obj   rom   

// 1:交换变量的值
let a = 1; let b = 2;
console.log(a,b);
[a,b] = [b,a];
console.log(a,b)
// 2:函数返回多个值
function fn (){
return [1,2,3]
}
let [a,b,c] = fn()
console.log(a,b,c)
function fn2(){
return {
a:1,
b:2
}
}
let {a:d,b:e} = fn2()
console.log(d,e)
// 3:函数参数的定义
function fn([a,b]){return a+b}
console.log(fn([1,2]))
function fn2({a,b}){return a+b}
console.log(fn2({b:1,a:2}))
// 4:提取json数据
let obj = {
a:2,
b:4
}
let {a,b} = obj
// 5:函数参数的默认值可以直接在()内定义而不必在函数内写
// 6:便利map结构
let map = new Map()
map.set(‘first‘,‘hello‘)
map.set(‘second‘,‘world‘)
for(let [key,val] of map){
console.log(key+‘ is ‘+val)
}
for(let [,val] of map){
console.log(val)
}
for(let [key] of map){
console.log(key)
}
// 7:引入模块
// data里数据
const data = {
a:1,
b:2
}
import {data} from ‘url/data‘

es6--解构赋值-用途

标签:模块   class   let   const   多个   return   key   obj   rom   

原文地址:http://www.cnblogs.com/ww93/p/8053838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!