-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaultfunctionargz.js
68 lines (49 loc) · 1.59 KB
/
defaultfunctionargz.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
function makeAjaxRequest(url, method) {
if (!method) {
method = 'GET';
}
//logic to make the request
}
makeAjaxRequest('google.com');
makeAjaxRequest('google.com', 'GET');
makeAjaxRequest('google.com', 'POST');
// refactor
// to make use of default function arguments, instead of the tedious
// check we did with the if statement above
function makeAjaxRequest(url, method = 'GET') {
//logic to make the request
}
//if you want the method to deliberately be empty, then
makeAjaxRequest('google.com', null); // method will not be reassigned to default
//if you use undefined instead of null, method will yes be reassigned to default
makeAjaxRequest('google.com', undefined);
// With default parameters, 0 or "" or null are valid values and will not
// be replaced by the default value.
// Below is a constructor function
function User(id) {
this.id = id;
}
// generate random id
function generateId() {
return Math.random() * 999999;
}
function createAdminUser(user) {
user.admin = true;
return user;
}
createAdminUser(new User(generateId()));
// refactor to accomodate whether a user parameter is passed in or not
function createAdminUser(user = new User(generateId())) {
user.admin = true;
return user;
}
createAdminUser();
//If I have a user already, I could pass in the user as well
const user = new User(generateId());
createAdminUser(user);
function getPonies(size = defaultSize(), page = size - 1) {
server.get(size, page)
}
const { timeout = 1000 } = httpOptions;
// timeout will be the value of httpOptions.timeout if it exists
// or 1000 if not