展开开语法(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]注意,这里实际上只拷贝了一层,属于浅拷贝,数组中的对象仍然是拷贝的引用。
Thinking back on our talk with Casey, top of mind I was impressed that Casey has a great balanced approach. 1. Being aware of her body, knowing when to rest, when she can train hard. 2. To love her body type and use it to her advantage such as sprint races 3. Have other interests. Thanks again #caseywright Branden Clovis
I and my buddies were actually reviewing the excellent information and facts on your web page and at once I had a horrible suspicion I never thanked you for them. My guys became excited to read all of them and already have in truth been taking advantage of those things. Thanks for genuinely indeed considerate and for making a decision on this form of nice areas most people are really wanting to discover. My personal sincere apologies for not expressing appreciation to sooner. Everett Schoettle
I in addition to my pals came viewing the nice secrets and techniques located on your web site then all of the sudden came up with a horrible feeling I never expressed respect to the site owner for those techniques. My ladies are already so thrilled to learn them and have now quite simply been having fun with these things. We appreciate you getting quite thoughtful and for choosing certain fantastic things most people are really desperate to discover. Our own honest regret for not expressing appreciation to sooner. Stanley Addy
Your blog is timely, Joe. I see my ship sailing forward from the horizon and getting closer. Your comments about security have been one of the priorities on my list this week. I am attempting to make sure all my ducks are in a row. With all things to be compliant in Compumatrix, and have security source updated and in place. Thus my writing about blog A Corporation or LLC. I thought about the trust which can be a stand-alone entity. Some trusts can have other trusts contained in them. You may purchase a trust under the umbrella of an LLC also. So much to consider when you have so much to consider. Terrence Montalvan
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention. Cleo Cayson