美文网首页
ES6中扩展运算符的应用

ES6中扩展运算符的应用

作者: 周星星的学习笔记 | 来源:发表于2021-03-28 15:00 被阅读0次

一、数组的合并

const arr1 = ['周星驰', '周杰伦', '周华健'];
const arr2 = ['刘德华', '周润发', '张学友'];
const arr3 = [...arr1, ...arr2];
console.log(arr3);
输出结果

二、数组的克隆(浅拷贝)

const arr1 = ['周星驰', '周杰伦', '周华健'];
const arr2 = [...arr1];
console.log(arr2);
输出结果

三、伪数组转换为真正的数组

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试</title>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <script>
        const divs = document.querySelectorAll('div');
        const arr = [...divs];
        console.log(arr);
    </script>
</body>
</html>
输出结果

相关文章

网友评论

      本文标题:ES6中扩展运算符的应用

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