Home > Web Front-end > JS Tutorial > Detailed explanation of the spread operator in ES6

Detailed explanation of the spread operator in ES6

php中世界最好的语言
Release: 2018-03-10 14:18:01
Original
1791 people have browsed it

This time I will bring you the expansion of ES6Operatordetailed explanation, what are the precautions for using the expansion operator of ES6, the following is a practical case, let's take a look.

//当传入的参数不确定时候;
function a(...arg) {
    console.log(arg[0]);
    console.log(arg[3]);
}
a(1, 23, 0);        //1  undefined
//其实也可以用这种替代; arguments本身是一个函数实参对象;
function a() {
    console.log(arguments[0]);
    console.log(arguments[3]);
}
a(1, 23, 0)            //1  undefined
//可以这么理解: ...arg取得了一个实参对象的每一项的值;并不是那个对象;
再看:
var arr0=["liu","hai"];
var arr1=[...arr0];
arr1.push("hello");
console.log(arr0);     //["liu","hai"]
console.log(arr1)      //["liu","hai","hello"]
发现并没有共享一份数据;
function test(aa,...args){
  console.log(aa);
  console.log(args[0]);
  console.log(args[1]);
  console.log(args.length);
}
test("first",1,2);       // first 1  2  2;
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!

Related reading:

Entry, output, module analysis of webpack3.x

##Vue’s 2.0 component registration details

The above is the detailed content of Detailed explanation of the spread operator in ES6. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template