zoukankan      html  css  js  c++  java
  • ES6

    1.回调使用promise,同步执行

     1     let pro = function(data){
     2         return new Promise((resolve,reject)=>{//使函数return对象,使能then
     3             setTimeout(()=>{
     4             console.log(data);
     5             resolve("hello!");
     6         },1000)
     7     })
     8     };
     9 
    10     pro(1).then(function(res){//res:接收成功 "hello!", then(()=>pro(2)) 不写{},自带return
    11         console.log(res);
    12         return pro(2);
    13     }).then(function(res){
    14         console.log(res);
    15         pro(3)});
    16 
    17     结果:
    18     1
    19     hello!
    20     2
    21     hello!
    22     3

    2.常量 const 定义变量大写

    3. 字符串用单引号或模板包裹,禁止双引号,模板字符串的使用

    4设置对象变量键值的语法

    let myKey = 'variableKey'
    let obj = {
        key1: '1',
        [myKey]: '2'
    }

    5.箭头函数尽量处理简单函数

    6.find/findIndex 代替 indexOf

    let ages = [12, 19, 6, 4];
    let firstAdult = ages.find(age => age >= 18);    // 19
    let firstAdultIndex = ages.findIndex(age => age >= 18);    // 1

    7.扩展运算符 替代 Array.from

    Math.min(2,3,4)    // 2
    console.log(Array.from('foo'));
    // expected output: Array ["f", "o", "o"]
    console.log(Array.from([1, 2, 3], x => x + x));
    // expected output: Array [2, 4, 6]
    
    
    let numbers = [2,3,4]
    Math.min(...numbers);    //2
    // 能够把可迭代对象( NodeList, arguments 等等) 转为真正的数组
    let divsArray = [...document.querySelectorAll('div')];

    8.数组去重,tips:[...new Set(array)]  同样适用于字符串去重 [...new Set(string)].join('')

     let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3];
     let set = new Set(array);
     console.log(set);
    
    // => Set {1, 2, 3, 4, 5}
    
    //配合 Array.from
    let array = Array.from(new Set([1, 1, 1, 2, 3, 2, 4]));
    console.log(array);
    // => [1, 2, 3, 4]

    9.少写 if else

    // 优雅,推荐
    const keyArr = ['1,2,3,4,5', '40'] const valArr = [f1, f2] const getVal = (param = '') => { let index = keyArr.findIndex( it => { return it.inclues(param) }) return valArr[index] } let a = 2 let handle = getVal(a) handle()
    // es
    const map1 = new Map()
    const statusArr = [1,2,3,4,5]
    map1.set(statusArr, f1)
    let handle = function () {}
    const getVal = (param = '') => {
        for (let value of map1.entries()) {
            console.log(JSON.stringify(value))
            if (value[0].inclues(param)) {
                 handle = value[1]
            }
        }
    }
    const a = 2
    getVal(a)
    handle()

    10.for-of,   tips: ES5  forEach, for-in   通用  for

    11.解构

    function fn2 ({x=0, y=0}={x:2, y:2}) {
        console.log(x, y)
    }
    fn2()   //解得  2  2
    fn2({})  //解得 0  0
  • 相关阅读:
    Transaction 'IREG', Abend 'APCT', at '????'.
    CICS中设置是否具有可修改性
    常用命令总结
    常用语句总结
    _func_
    数组排序返回索引-python和c++的实现
    数组的并行求和-cuda实现
    Pytorch 多GPU训练-多计算节点并行-All you need
    Pytorch 多GPU训练-单运算节点-All you need
    Windows 编程中的问题
  • 原文地址:https://www.cnblogs.com/yuqlblog/p/8021967.html
Copyright © 2011-2022 走看看