公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:52010
  • 待审:67
  • 小程序:12
  • 文章:1106242
  • 会员:784

从现有数组形成一个新数组,字符噪音更少。
spread 将元素提取为值。它是一个不可变的函数。
[] 是写入新数组的方式。因此,展开不会改变原始数组。
spread 适用于所有可迭代对象,而不仅仅是数组。
iterables:字符串、数组、映射、集合,即除了对象数据类型之外的大多数内置数据结构。
与解构的区别:从数组中提取所有元素,并且不创建新变量,仅在需要值 csv 的地方使用。
当我们构建数组或将值传递给函数时使用。

const nums = [5,6,7];
const newnums = [1,2, nums[0],nums[1],nums[2]];
console.log(newnums); // [ 1, 2, 5, 6, 7 ]

is reduced to 

const nums = [5,6,7];
const newnums = [1,2, ...nums];
console.log(newnums); // [ 1, 2, 5, 6, 7 ]
console.log(...newnums); // 1 2 5 6 7

登录后复制

1. 将两个数组合并在一起

const arr1 = [1,2,3,4,5];
const arr2 = [6,7,8,9];

let nums = [...arr1,...arr2];
nums; // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]


const firstname = "peter";
const fullname = [...firstname,' ',"p."];
fullname; // [ 'p', 'e', 't', 'e', 'r', ' ', 'p.' ]

console.log(...firstname); // 'p' 'e' 't' 'e' 'r'

登录后复制

错误来源:扩展运算符在模板字符串中不起作用,因为模板字符串不希望其中有多个值。

2. 创建数组的浅拷贝

const girl = {
  name: 'melania',
  friends: ['alina', 'alice', 'ayesha', 'anamika', 'anaya']
};

const frnz = [...girl.friends];
console.log(frnz); // [ 'alina', 'alice', 'ayesha', 'anamika', 'anaya' ]
console.log(girl.friends); // [ 'alina', 'alice', 'ayesha', 'anamika', 'anaya' ]

登录后复制

spread 也适用于对象,即使它们不是可迭代的。

let male = {
  "firstname": "gangadhar",
  "lastname": "shaktimaan"
}

let female = {
  "firstname": "geeta",
  "lastname": "vishwas"
}

let x = {...male, born: 1950};
let y = {...female, born: 1940};

x; // { firstname: 'gangadhar',   lastname: 'shaktimaan',   born: 1950 }
y; // { firstname: 'geeta',  lastname: 'vishwas',  born: 1940 }```




## shallow copy of objects:

let male = {
  "firstname": "gangadhar",
  "lastname": "shaktimaan"
}

let character = {...male, firstname: 'wonderwoman'};

male;         // { firstname: 'gangadhar', lastname: 'shaktimaan' }
character;    // { firstname: 'wonderwoman', lastname: 'shaktimaan' }

- first name for character object is changed although it was extracted from male object


登录后复制

休息模式和休息参数:

rest 的作用与 spread 相反,但具有与 spread 相同的语法。
spread 用于构建新数组或将值传递给 fn。在这两种情况下,扩展都用于扩展元素。
rest 使用相同的语法,但将这些值压缩为一个
spread 用于从数组中解包元素,rest 用于将元素打包到数组中。
““

展开运算符和剩余运算符的语法差异:
扩展运算符 => … 用于赋值运算符的 rhs。
const nums = [9,4, …[2,7,1]];

剩余运算符=> …将位于带有解构的赋值运算符的lhs上
const [x,y,…z] = [9,4, 2,7,1];

## rest syntax collects all the elements after the last elements into an array. doesn't include any skipped elements. 
- hence, it should be the last element in the destructuring assignment.
- there can be only one rest in any destructuring assignment.

登录后复制

let diet = [‘披萨’, ‘汉堡’,’面条’,’烤’,’寿司’,’dosa’,’uttapam’];

让[第一,第三,…其他] =饮食;
首先;
第三;
其他;

- rest also works with objects. only difference is that elements will be collected into a new object instead of an arrray.

登录后复制

let days = { ‘mon’:1,’tue’:2,’wed’:3,’thu’:4,’fri’:5,’sat’:6,’sun’:7};
让{周六,周日,…工作} = 天;
放开= {周六,周日};

工作; // { 周一:1,周二:2,周三:3,周四:4,周五:5 }
离开; // { 周六: 6, 周日: 7 }

- although both look same, but they work differently based on where they are used.

登录后复制

休息和传播的微妙区别:

扩展运算符用于我们写入用逗号分隔的值
使用休息模式,我们将编写用逗号分隔的变量名称。


登录后复制

分享到:
标签:休息 传播 运算符
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 52010

    网站

  • 12

    小程序

  • 1106242

    文章

  • 784

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定