es6基础知识

时间:2017-10-05 09:31:18   收藏:0   阅读:272

1、箭头操作符

var arr=[1,5,6]
//传统写法
arr.forEach(function(v){
  console.log(v)
})
//箭头操作符
arr.forEach(v=> console.log(v))

2、字符串模板(` `)

var str =Math.random()

console.log(`you num is ${str}`) 

3、解构

//函数解构
var [x,y] = getVal()

function getVal(){
   return [5,6] 
}

// 数组解构

let  [‘name‘,‘age‘] = [‘vhen‘,15]
console.log(‘x:‘+x+‘, y:‘+y);//输出:x:5, y:6
console.log(‘name:‘+name+‘, age:‘+age);//输出: name:vhen,age:15 

4、参数默认值,不定参数

//默认参数
function hello (name=‘vhen‘){
    console.log(`Hello ${name}`)
}
//不定参数
 function add (...arg){
    return  arg.reduce((m,n )=> m+n);
}
console.log(add(5,6))

5、Set数据解构(成员的值都是唯一的,没有重复的值)  

      

var s = new Set();
 
s.add(‘vhen‘).add(18) //  vhen,18

s.size===2 //true

s.has(‘vhen‘)  //true

s.delete(‘vhen‘)

     Array.from方法可以将 Set 结构转为数组

 

const s=new Set([5,6,8,9])
const arr=Array.from(s)

6、Map数据结构

原文:http://www.cnblogs.com/vhen/p/7628162.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!