展开开语法(Spread syntax)是属于 ES6 的一个新特性, 可以将数组表达式或者字符串在语法层面展开。用法:
myFunction(...iterableObj)接下来我们看看这个新特性到底有什么用
apply
下面的方法传入3个参数然后计算和
function sum (a,b,c) {
return a+b+c
}如果我们要传入的参数是一个数组呢?
var arr = [1,2,3]通常我们会用 apply() 来实现
sum.apply(null, arr)但是有了展开运算符我们实现起来就更简单了
sum(...arr)代码看起来是不是更简洁清晰了?
数组合并
合并数组的方法有很多,常用的是 concat()
var arr1 = [1,2]
var arr2 = [3,4]arr1 = arr1.concat(arr2)需要注意的是,concat是返回一个新的数组,并非修改原数组,所以还需要去接收返回值。
然而用 push (向数组的末尾添加一个或多个元素)结合展开运算符是能直接修改原数组的,非常酷炫
arr1.push(...arr2)或者追加到数组的开头
arr1.unshift(...arr2)有没有更变态的呢?把新数组添加到任意位置
var arr2 = [3, ...arr1, 4]非常灵活方便。
上面提到的
push()方法实际上是属于一类可接收任意数量参数的方法,而此类方法均可以使用展开运算符
比如 Math.min()
var nums = [4, 5, 3, 1]
Math.min(...nums)数组拷贝
var arr1 = [1,2]
var arr2 = [...arr1]注意,这里实际上只拷贝了一层,属于浅拷贝,数组中的对象仍然是拷贝的引用。
Hello. Allow me to introduce the author. Her name is Mahalia Buchholtz. I am currently a procurement officer and the salary been recently really accomplishing. Solving puzzles is what she loves doing. Vermont has always been my living place having said that i will always be move annually or a couple. If you want to find out more away my website: #link# Kermit Lanois
Very good point which I had quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Holisticly restore superior interfaces before flexible technology. Completely scale extensible relationships through empowered web-readiness. Howard Svare
The subsequent time I learn a weblog, I hope that it doesnt disappoint me as much as this one. I imply, I do know it was my option to read, however I really thought youd have something interesting to say. All I hear is a bunch of whining about one thing that you might fix if you happen to werent too busy looking for attention. Fermin Rhodehamel
I want to show my respect for your kind-heartedness for those people who actually need guidance on this particular concept. Your very own commitment to getting the message all through came to be pretty helpful and have usually enabled guys and women just like me to arrive at their objectives. Your entire helpful hints and tips can mean a great deal to me and a whole lot more to my mates. With thanks; from all of us. Mario Culbreth
Great article. It is extremely unfortunate that over the last several years, the travel industry has already been able to to take on terrorism, SARS, tsunamis, bird flu virus, swine flu, along with the first ever real global downturn. Through everything the industry has really proven to be powerful, resilient and dynamic, discovering new solutions to deal with misfortune. There are always fresh challenges and opportunity to which the marketplace must yet again adapt and react. Zane Gastelo