Skip to content

Commit a183471

Browse files
committed
ES6 parameter handling example
1 parent b45441d commit a183471

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="14-ES6ParameterHandling.js"></script>
9+
</body>
10+
</html>

chapter01/14-ES6ParameterHandling.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

Comments
 (0)