1
+ //******* EcmaScript 6: Default Parameter Values
2
+ function sum ( x = 1 , y = 2 , z = 3 ) {
3
+ return x + y + z
4
+ } ;
5
+ console . log ( sum ( 4 , 2 ) ) ; //outpus 9
6
+
7
+ //function above is the same as
8
+ function sum2 ( x , y , z ) {
9
+ if ( x === undefined )
10
+ x = 1 ;
11
+ if ( y === undefined )
12
+ y = 2 ;
13
+ if ( z === undefined )
14
+ z = 3 ;
15
+ return x + y + z ;
16
+ } ;
17
+ console . log ( sum2 ( 4 , 2 ) ) ; //outpus 10
18
+
19
+ //******* EcmaScript 6: spread operator ('...')
20
+ var params = [ 3 , 4 , 5 ] ;
21
+ console . log ( sum ( ...params ) ) ;
22
+
23
+ var numbers = [ 1 , 2 , ...params ] ; //pushing values into array
24
+ console . log ( numbers ) ;
25
+
26
+ //******* EcmaScript 6: rest parameter ('...')
27
+ function restParamaterFunction ( x , y , ...a ) {
28
+ return ( x + y ) * a . length ;
29
+ }
30
+ console . log ( restParamaterFunction ( 1 , 2 , "hello" , true , 7 ) ) ; // outputs 9;
31
+
32
+ //code above is the same as ES5:
33
+ function restParamaterFunction ( x , y ) {
34
+ var a = Array . prototype . slice . call ( arguments , 2 ) ;
35
+ return ( x + y ) * a . length ;
36
+ } ;
37
+ console . log ( restParamaterFunction2 ( 1 , 2 , "hello" , true , 7 ) ) ;
0 commit comments