-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
163 lines (145 loc) · 4.39 KB
/
app.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* functions ------------------------------------------------------------------
> parameters - regular, default, ...rest
> arguments - regular, ...spread
> return
*/
function display(regularPara, defaultPara= 'World', spreadChar1, spreadChar2, spreadChar3, ...restPara) {
console.log(regularPara + ' ' +defaultPara);
restPara.forEach(name => console.log(
regularPara + ' ' + name + ' ' + spreadChar1+spreadChar3+spreadChar2 + ' !'));
}
let spreadPara = ['a', 'b', 'c'];
display('Hello');
display('Hello', 'Everyone');
display('Hello', undefined, ...spreadPara, 'Mary', 'John', 'Smith');
display('Hello', 'Everyone', ...spreadPara, 'Mary', 'John', 'Smith');
/* function scope
> outer/parent function
> nested/inner function
*/
/* block scope - ES2015
> within {} (if/while/for etc any other than function)
> *let vs var
*/
console.log('1 ------------------------------------------------------------- ');
// IIFE - immediatly invoked function expression
(function () {
console.log("This is IIFE");
})();
// IIFE > assign to variable to make function expression
let greeting = (function () {
let message = "Hello";
let getMessage = function () {
return message;
};
})();
//console.log(greeting.message); // undefined
// closures > hold on even after function excute
let greetingClosure = (function () {
let message = "Hello";
let getMessage = function () {
return message;
};
return {
getMessage: getMessage,
};
})();
console.log(greetingClosure.getMessage()); // Hello
// closure example
function setupCounter(val) {
return function counter() {
return val++;
};
}
let counter1 = setupCounter(0);
console.log(counter1()); // 0
console.log(counter1()); // 1
let counter2 = setupCounter(10);
console.log(counter2()); // 10
console.log(counter2()); // 11
console.log(counter1()); // 2
console.log(counter2()); // 12
console.log('2 ------------------------------------------------------------- ');
/* arrow function
> single param
> this
*/
let helloObject = {
first: "John",
regularFunction: function (last) {
console.log(this);
console.log("Hello " + this.first + last);
},
arrowFunction: (last) => {
console.log(this);
console.log("Hello " + this.first + last);
},
};
helloObject.regularFunction("Smith");
helloObject.arrowFunction("Smith");
console.log('3 ------------------------------------------------------------- ');
/* understanding JS `this` keyword
* this = executing content of block scope
*/
// Example 1
function sayHi1() {
console.log("Hi_1");
console.log(this);
}
sayHi1(); // Hi // window {...}
// Example 2
let greeting2 = {};
greeting2.sayHi2 = function () {
console.log("Hi_2");
console.log(this);
};
greeting2.sayHi2(); // Hi // {sayHi: f}
// Example 3
function sayHi3() {
console.log("Hi_3");
console.log(this);
}
let greeting3 = new sayHi3(); // Hi // [obj Object]
/* understanding `call / apply` method
* > `call` - individual arguments of varying type
* > `apply` - array inputs with simillar elements
*/
let person1 = { firstName: "John", age: 21 };
let person2 = { firstName: "Will", age: 22 };
let sayCallHi = function (lastName, profession) {
console.log(
this.firstName + ", " + lastName + ", " + this.age + ", " + profession
);
};
sayCallHi("Smith"); // undefined, Smith, undefined, undefined
sayCallHi.call(); // undefined, undefined, undefined, undefined
sayCallHi.call(undefined, "Smith"); // undefined, Smith, undefined, undefined
sayCallHi.call(person1, "Smith", "Lawyer"); // John, Smith, 21, Lawyer
sayCallHi.call(person2, "Smith", "Doctor"); // Will, Smith, 22, Doctor
sayCallHi("Smith"); // undefined, Smith, undefined
sayCallHi.apply(); // undefined, undefined, undefined
sayCallHi.apply(undefined, ["Smith", "Lawyer"]); // undefined, Smith, undefined, Lawyer
sayCallHi.apply(person1, ["Smith", "Lawyer"]); // John, Smith, 21, Lawyer
sayCallHi.apply(person2, ["Smith", "Doctor"]); // Will, Smith, 22, Doctor
console.log('4 ------------------------------------------------------------- ');
/* understanding `bind` method
*/
let personFemale = {
name: "Mary",
getName: function() {
return this.name;
}
};
let personMale = {
name: "John"
};
let getNameCopy = personFemale.getName.bind(personMale);
console.log(getNameCopy);
console.log(getNameCopy());
/* Built-in functions --------------------------------------------------------
> eval()
> parseInt
> parseFloat
> escape
> unescape
*/