ES6 扩展运算符

介绍

本文是在学习ES6时做的一些学习笔记,所有的笔记请查看:ES6 学习笔记

扩展运算符

扩展运算符示例功能如下:
1、对象中的扩展运算符(…)用于取出参数对象中的所有可遍历属性,拷贝到当 前对象之中
2、整合两个数组
3、扩展运算符复制数组

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spread operator Intro</title>
</head>

<body>
<script>
const youngers = ['Geroge','John','Thomas'];
const olders = ['James','Adrew','Martin'];

// 整合两个数组
const members = youngers.concat(olders); //传统写法
console.log(members);

// 在整合的数组中添加一个元素,传统写法
let members = [];
members.concat(youngers);
members.push('Mary');
members=members.concat(olders);
console.log(members)

// es6 扩展运算符写法如下
let members=[...youngers,'Mary',..olders]

// 扩展运算符复制数组
const currentMembers=[].concat(members) // 传统写法
const currentMembers = [...members] // 扩展运算符写法
</script>

</body>
</html>

简单效果:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spread operator Intro</title>
</head>
<style>
.heading span{
    cursor:pointer;
    dispaly:inline-block;
    transition:transform 0.25s
    
}
.heading span:hover{
    transofrm:translateY(-20px) rotate(10deg) scale(2)
}
</style>

<body>
<h2 class='heading'> LARAVIST!</h2>
<script>
const heading=document.querySelector('.heading')
heading.innerHtml=wrapWithSpan(heading.textContent);

function wrapWithSpan(word){
    return [...word].map(letter => `<span>${letter}<span>`)
    .join('');
}

</script>

</body>
</html>

扩展运算符例子:
1、使用扩展运算符,解决document.querySelectorAll()返回的nodelist
不是数组得问题,替换了Array.from(),因为nodelist为可迭代对象
2、扩展对象的属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spread operator Intro</title>
</head>
<body>
<ul>
 <li>go to store</li>
<li>watch tv</li>
<li>go shopping</li>
</ul>

<script>
// 使用扩展运算符,解决document.querySelectorAll()返回的nodelist
// 不是数组得问题,替换了Array.from(),nodelist为可迭代对象,
// 使用扩展操作符...进行操作
const todos=[...document.querySelectorAll('li')]
const names = todos.map(todo=>todo.textContent)
console.log(todos)

// 扩展对象的属性
const favorites = {
    color: ['yellow','blue'],
    fruits: ['banana','mongo'],
}
const shoppinglist = ['milk','sweets',...favorites.fruits]// 根据指定id删除数组中的某个值
const todos=[
    {id: 1,name: 'go to store',completed: false},
    {id: 2,name: 'watch tv',completed: true},
    {id: 3,name: 'Go shopping',completed: false}
]
const id=2;
const todoIndex=todos.findIndex(todo=>todo.id === id)
// 使用扩展运算符,将数组中的每一项扩展到新数组中
const newtodos=[...todos.slice(0,todoIndex),...todos.slice(todoIndex+1)];
</script>

</body>
</html>

将扩展运算符应用于函数上:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spread operator Intro</title>
</head>
<body>
<ul>
 <li>go to store</li>
<li>watch tv</li>
<li>go shopping</li>
</ul>

<script>

const fruit = ['apple','banana','pear'];
const newfruit = ['orange','mongo'];
// 将newfruit的内容放入fruit里面
fruit.push.apply(fruit,newfruit); //传统方法,将newfruit的内容存入fruit数组中
fruit.push(...newfruit); // es6方法,将newfruit扩展为参数

const dateFields = [2017,5,6];
// 传统方法
const date=new Date(dateFields[0],dateFields[1],dateFields[2]);
// 使用es6方法,使用扩展运算符,不用一个一个的调用字段
const date=new Date(...dateFields)
console.log(date)
                                   

</script>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值