-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_function_arguments.js
101 lines (81 loc) · 1.65 KB
/
default_function_arguments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
////////////////////////////////////
// ES6 - Default function arguments
////////////////////////////////////
/*
function makeAjaxRequest(url, method) {
if (!method) {
method = 'GET';
}
//Logic to make the request
}
makeAjaxRequest('google.com');
makeAjaxRequest('google.com', 'GET');
function makeAjaxRequest(url, method = 'GET') {
return method;
//Logic to make the request
}
console.log(makeAjaxRequest('google.com'));
console.log(makeAjaxRequest('google.com', 'POST'));
*/
// ==========================================================
/*
// ES5
function User(id) {
this.id = id;
}
var UserIsak = new User(1);
function generateId() {
return Math.random() * 9999999;
}
function createAdminUser(user) {
user.admin = true;
return user;
}
createAdminUser(new User(generateId()));
// ES6 with default function arguments
function User(id) {
this.id = id;
}
var UserIsak = new User(1);
function generateId() {
return Math.random() * 9999999;
}
function createAdminUser(user = new User(generateId())) {
user.admin = true;
return user;
}
const userTest = new User(generateId())
console.log(createAdminUser(userTest));
*/
// ===================================
/*
// ES5
function sum(a, b) {
if (a === undefined) {
a = 0;
}
if (b === undefined) {
b = 0;
}
return a + b;
}
// ES6
function sum(a = 0, b = 0) {
return a + b;
}
*/
// ==================================
//Dumping Unused Code
// ES5
function addOffset(style) {
if (!style) {
style = {};
}
style.offset = '10px';
return style;
}
// ES6
function addOffset(style = {}) {
style.offset = '10px';
return style;
}