es6

consECMAScript 6

javascript 语言的国际化标准

1、不存在变量提升

console.log(a)

let a = 2

2、let在声明之前都是不可用的 

    typeof不再是100%不报错

3、不能重复声明

4、块级作用域

    //  内层变量覆盖外层

    var a = 123;

    function fn () {

        console.log(a)

        if(false) {

            var a = 456;

        }

    }

    fn() //  undefined

    //  块级作用域

    {

        {

        let a = 2

        }

        console.log(a) // a is not defined

    }


    {

          let a = 2

          {

            console.log(a) //  2

          }

        }

内部可以访问外部

每一层都是一个单独的作用域


// const 只读变量,只能定义一次,必须有初始值

const a = 123

// let 

 数组结构赋值

let [a, b] = [1, 2] // a = 1, b = 2

let [a, [[b], c]] = [1, [[2], 3]] //  a = 1, b = 2, c = 3
// 不完全解构

let [ , , three] = [1, 2, 3] //  three = 3

let [x, y] = [1, 2, 3] // x = 1, y = 2

//  默认值

let [a = true] = [] // a = true

let [b = 1] = [undefined] //  b = 1

let [c = 2] = [null] // c = null

对象解构赋值

let {a, b} = {a: '123', b: '456'} // a = 123, b = 456

let {a, b} = {b: '456', a: '123'} // a = 123, b = 456

let {c} = {b:123} //  c = undefined

let {log} = console

log('hello word') //  'hello word'

// 找同名属性 然后赋值

// 嵌套

let obj = {

    f: [

        '123',

        {h: 'hhh'}

     ]

}

let {f:[v,{h}]} = obj //  v = '123', h = 'hhh'

字符串的扩展

let str = `这是字符串`

//引入变量

let a = '123'

let str = '这是字符串 ${a}' 

console.log(str)//  这是字符串123


//调用函数

function fn() {

    return '456'

}

let str = '这是字符串 ${fn()}' 

console.log(str)//  这是字符串456


//做嵌套

let arr = [7,8,9] 

const fn = ()=>` 这是字符串${arr.map(v=>v)} ` 

console.log(fn()) //  这是字符串7,8,9


//es5 indexOf

//includes() 返回 布尔值 是否包含

let str = 'hello word'

console.log(str.includes('o')) //  true

//  startsWith() endsWith ()  是否在头/尾部

console.log( str.startsWith('h') ) //  true

console.log( str. endsWith('d') ) //  true


//  repeat(n) 重复n次(向下取整) 小于等于-1报错

let str = 'a'

console.log(s.repeat(3)) //  aaa

数组的扩展

// ... 扩展运算符 // 拆包 打包

console.log(...[1,2,3]) //  1 2 3


//  函数调用

function add (x,y) {

    console.log(x+y)

}

let arr = [1,2]

add(...arr) // 3


// 接收表达式

let arr = [1, ...(1>0 ? ['a'] : [])]

console.log(arr) //  [1,  'a']


//  替代apply

let arr1 = [1, 2]

let arr2 = [3, 4]

console.log(arr1.concat(arr2)) //  [1, 2, 3, 4]

console.log(arr) //  [1, 2]

Array.prototype.push.apply(arr, arr2)  // 4

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

arr1.push(...arr2)

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

//  打包

let arr1 = [1, 2, 3]

let arr2 = [4, 5, 6]

let arr3 = [7, 8, 9]

let arrAll = [...arr1, ...arr2, ...arr3]

console.log(arrAll) //  [1, 2, 3, 4, 5, 6, 7, 8, 9]

//  浅拷贝

set数据结构

// Number  String Boolean Undefined Null (typeof)

// Array Object (instanceof, constructor, Object.prototype.toString, isArray)

let arr = []

let obj = {}

//instanceof 

console.log(arr instanceof Array) // true

console.log(obj instanceof Object) // true

//constructor 

console.log(arr.constructor === Array) // true

console.log(obj.constructor === Object) // true

// Object.prototype.toString

Object.prototype.toString.call(arr) === "[object Array]" // true

Object.prototype.toString.call(obj) === "[object Object]" // true 

// isArray

Array.isArray(arr)// true 

Array.isArray(obj)// false


// set 类似于数组 成员的值是唯一的

let res = new Set([1,2,3,4,5,6,1,2,3,4,5,6])

Object.prototype.toString.call(res)  // "[object Set]"

console.log(res) //  Set(5) {1, 2, 3, 4, 5, 6}

// 怎么转换成数组

// 1 部署了Iterator 对象 比如 Set 可以for of

// 类数组对象 转化成数组 必须要有length属性

    let obj = {

        0: 'a',

        1: 'b',

        2:'c',

        length: 3

    }

let resObj = Array.from(obj) // ['a', 'b', 'c']

let result = Array.from(res) //  [1, 2, 3, 4, 5, 6]

// ... 展开运算符

console.log([...res]) //  [1, 2, 3, 4, 5, 6]


// set属性 size add delete has clear

console.log(res.size) // 6 唯一成员值

let set1 = new Set()

set1.add(1).add(2).add(2)

console.log(set1) //  Set(2) {1, 2}

console.log(res.has(3)) // false 是否包含

set1.delete(1) // true 删除成功true 失败false

set1.clear()

console.log(set1) // Set(0) {}


// set方法 keys values entries

let set = new Set(['red', 'green', 'blue'])

console.log(set.keys()) // SetIterator {"red", "green", "blue"}

console.log(set.values()) // SetIterator {"red", "green", "blue"}

for(let i of set.keys()) { 

    console.log(i) // red green blue

}

for(let j of set. entries()) {

    console.log(j) // ['red', 'red'] ['green', 'green'] [''blue', 'blue'] 

}


// 交集并集差集

let a = new Set([1, 2, 3])

let b = new Set([4, 3, 2])

//  并集

let unique = new Set([...a, ...b])

console.log(unique) // Set(4) {1, 2, 3, 4}

//  交集 has

let inter = new Set([...a].filter(x=>b.has(x)))

console.log(inter) // Set(2) {2, 3}

//  差集 has

let dif = new Set([...a].filter(x=>!b.has(x)))

console.log(dif) // Set(1) {1}   

map数据结构

// map 新型数据结构

let o1 = {a: 1} //  [object Object]

let o2 = {b: 2} //  [object Object]

let o3 = {} 

o3[o1] = 1

o3[o2] = 2

console.log(o3) // {[object Object]: 2}

let map = new Map()

map.set(o1, '111')

map.set(o2, '222')

console.log(map)

console.log(map)

你可能感兴趣的:(es6)