ES6

作者: 蔷薇心情 | 来源:发表于2018-05-28 15:58 被阅读0次

解构

    数组:

        let [a,b,c] = [12,5,6];

        console.log(a)  //12

        //注意:左右两边结构保持一致

    json:

        let json = {

            name:'Tom',

            age:18

         }

        let {name,age} = json;

        解构可以给默认值:

        let [a,b,c="默认值"] = ['aaa','bbb']

        交换两个数的值

        let a = 12;

        let b = 5;

        [a,b] = [b,a];

        import {a,b,c} from './mod'

        function show({a,b="默认值"}){

            console.log(a,b);

        }

        show({a:1})

字符串模板:

        let a = ‘呵呵${name}’

        str.includes(要找的东西) 返回true、false

        str.startsWith(以谁开头)  返回true、false

        str.endsWith(以谁结尾)  返回true、false

函数:

    1、参数默认值:

        function show(a="欢迎",b="你好"){

        }

    2、函数参数默认是已经定义了,不能再重复定义

扩展运算符:

    var arr = ['banana','apple']

    ...arr  展开数组  输出:banana apple

剩余运算符:

    function show(a,b,...c){

        console.log(c)  //[3,4,5]

    }

    show(1,2,3,4,5)

    var arr = [1,2,3]

    var arr2 = [...arr]; //复制了一份

    [1,2,3]  -> ...[1,2,3]  -> 1,2,3

    1,2,3  ->  ...1,2,3  ->  [1,2,3]

箭头函数:

    () => {

    }

    1、this问题,定义函数所在的对象,不再是运行时所在的对象

    2、箭头函数里面没有arguments,用...

    3、箭头函数不能当构造函数

数组循环

    arr.forEach 代替普通的for循环

        arr.forEach(function(val,index,arr){

    })

    arr.filter()  //根据条件过滤返回 return val.id == 1;

    arr.some() //类似查找 找到返回true

    arr.map() //批量整理数据的结构 return返回数组

    arr.foEach(callback,指向的对象)

    Array.from(类数组)

    作用:把类数组(获取一组标签,arguments)转成数组

    arr.find((val,index,arr)=>{})  //查找,找出第一个符合条件的值

    arr.findIndex()://查找,返回的是位置

    arr.fill('默认值',start,end)

数组去重:

    new Set(arr)数组去重 但返回值是类数组

    Arry.from(new Set(arr))

对象新增

    {a,b,show(){}}  等同于  {a:a,b:b,show:function(){}}

    Object.assign 对象合并

    Object.assign(json1,json2)

    用途:

    1、复制一个对象

    2、合并参数

promise:

        new Promise(step1).then(function(val){

                console.info(val);

                return new Promise(step2);

            }).then(function(val){

                console.info(val);

                return new Promise(step3);

            })

            //es5实现promise

            MyPromise.prototype.then = function(resolveFunc, rejectFunc) {

                var self = this;

                return new MyPromise(function(resolve_next, reject_next) {

                    function resolveFuncWrap() {

                  var result = resolveFunc(self.value);

                  resolve_next(result);

                    }

                    function rejectFuncWrap() {

                    var result = rejectFunc(self.value);

                  resolve_next(result);

                    }

                    self.resolveFunc = resolveFuncWrap;

                    self.rejectFunc = rejectFuncWrap;

                })

                }

class类:

    class Person{

        constructor(name,age){

        this.name = name;

        this.age = age;

     }

    sayHello(){

        return this.name;

     }

    }

    class Children extends Person{

        constructor(){

        super(name)

        }

    }

相关文章

网友评论

    本文标题:ES6

    本文链接:https://www.haomeiwen.com/subject/mksgjftx.html