Some new features have also been added in the direction of function expansion. I feel that these features are also very important.
1. Parameter default values (note: the default value cannot be followed by Add parameters without default values)
{ function test(x, y = 'world'){ console.log('默认值',x,y); } test('hello');//hello world test('hello','kill');//hello kill }
{ let x='test'; function test2(x,y=x){ console.log('作用域',x,y); } test2('kill');//kill kill 这里涉及到作用域的问题 函数里面具有单独的作用域 只有没有x的时候 才会继承let所声明的x }
2. Rest parameters (...) Convert a series of discrete values into Array Similarly, rest cannot be followed by any parameters
{ function test3(...arg){for(let v of arg){ console.log('rest',v); } } test3(1,2,3,4,'a'); }
3. The spread operator (...) converts an array into a series of discrete values
{ console.log(...[1,2,4]); console.log('a',...[1,2,4]); }
4. Arrow function (very important, otherwise some new codes cannot be understood!!!) For example, a=> ;a*2 a is the parameter a*2 is the return value => as a symbol of the function When no parameters are passed, () can be used to represent
{ let arrow = v => v*2; let arrow2 = () => 5; console.log('arrow',arrow(3));//6 console.log(arrow2());//5 }
5. Tail call If a function nests another function, you can consider tail call
{ function tail(x){ console.log('tail',x); } function fx(x){return tail(x) } fx(123)// tail 123 }
The above is the detailed content of Example tutorial of function extension in ES6. For more information, please follow other related articles on the PHP Chinese website!