-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
96 lines (82 loc) · 2.27 KB
/
index.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
(function(context) {
const personJohnDoe = {
firstName: 'John',
lastName: 'Doe',
age: 28,
getFullName: function() {
console.log(`${this.firstName} ${this.lastName}`);
}
}
const personJamesBond = {
firstName: 'James',
lastName: 'Bond',
};
function print(shouldIndent) {
const lb = shouldIndent ? '\n' : '';
console.log(`${this.firstName} ${lb}${this.lastName}, ${lb}${this.age}`);
}
function greet(language, firstName, lastName) {
let greet = 'Hello';
if (language === 'es') {
greet = 'Hola';
}
console.log(`${greet}, ${firstName} ${lastName}`);
}
function bindDemo() {
console.log('\nBIND ----------');
print.bind(personJohnDoe)();
personJohnDoe.getFullName();
personJohnDoe.getFullName.bind(personJamesBond)();
const printJohnDoeWithIndent = print.bind(personJohnDoe, true);
printJohnDoeWithIndent();
}
function callDemo() {
console.log('\nCALL ----------');
print.call(personJohnDoe);
print.call(personJohnDoe, true);
personJohnDoe.getFullName.call(personJamesBond);
}
function applyDemo() {
console.log('\nAPPLY ----------');
print.apply(personJohnDoe);
print.apply(personJohnDoe, [true]);
greet.apply(this, ['es', 'Sumeet', 'Sarkar']);
const arr1 = [1, 2, 3, 4];
const arr2 = [5, 6, 7, 8];
arr1.push.apply(arr1, arr2)
console.log(arr1);
console.log('name: ', getConfig('name'));
console.log('server timeout: ', getConfig('server', 'timeout'));
}
function getConfig() {
return getConfigWithCountryCode.apply(this, [countryCode, ...Array.from(arguments)]);
}
const countryCode = 'uk';
const config = {
'us': {
name: 'United States Of America',
server: {
timeout: '300'
},
},
'uk': {
name: 'United Kingdom',
server: {
timeout: '500'
}
},
}
function getConfigWithCountryCode(countryCode = 'us', key1, key2) {
const countryConfig = config[countryCode];
if (key1 && key2)
return countryConfig[key1] && countryConfig[key1][key2];
return countryConfig[key1];
}
function demo() {
console.log('\n\nBIND CALL APPLY');
bindDemo();
callDemo();
applyDemo();
};
(context || this).demoLibs['bind-call-apply'] = demo;
})(window);