zoukankan      html  css  js  c++  java
  • ES6知识点整理之----数组扩展----概念及运用

    1、扩展运算符

    扩展运算符(spread)是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。

    console.log(...[1, 2, 3])
    // 1 2 3
    
    console.log(1, ...[2, 3, 4], 5)
    // 1 2 3 4 5
    
    [...document.querySelectorAll('div')]
    // [<div>, <div>, <div>]

    2、由于扩展运算符可以展开数组,所以不再需要apply方法,将数组转为函数的参数了

    // ES5 的写法
    function f(x, y, z) {
      // ...
    }
    var args = [0, 1, 2];
    f.apply(null, args);
    
    // ES6的写法
    function f(x, y, z) {
      // ...
    }
    let args = [0, 1, 2];
    f(...args);

    3、扩展运算符应用

    • 复制数组
    • 合并数组
    • 与解构赋值结合
    • 将字符串转为真正的数组
    • 实现了Iterator接口的对象,都可以使用扩展运算符转为真正的数组
    • Map、Set,Generator函数,都具有Iterator接口的对象,都可以使用扩展运算符
  • 相关阅读:
    Postman post csrf_token
    CBV
    nginx+uWSGI+django部署web服务器
    JS绘图
    get_字段_display()
    limit_choices_to
    window.onload=function(){};
    模拟百度搜索项目
    事件冒泡
    解绑事件
  • 原文地址:https://www.cnblogs.com/adhehe/p/9645767.html
Copyright © 2011-2022 走看看