-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathellie.js
186 lines (142 loc) · 3.54 KB
/
ellie.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Nullish Coalescing
function printMessage1(text) {
let message = text;
if(message === null || message === undefined) {
message = 'Noting to display 😜';
}
console.log(message)
}
function printMessage2(text) {
const message = text ?? 'Noting to display!' // 왼쪽 코드가 undefined, null일 경우 오른쪽 코드 실행
console.log(message)
}
function printMessage3(text = 'Noting to display') {
console.log(text); // undefined 경우에만 default parameter 사용 가능
}
function printMessage4(text) {
const message = text || 'Noting to display!' // 왼쪽 값이 falsy(undefined, null, false, 0, -0, NaN, ''..)인 경우 오른쪽 실행
console.log(message)
}
let printMessage = (func) => {
func('Hello');
func(undefined);
func(null);
func(0);
func('')
};
printMessage(printMessage1);
// Hello
// Noting to display 😜
// Noting to display 😜
// 0
//
printMessage(printMessage2);
// Hello
// Noting to display!
// Noting to display!
// 0
//
printMessage(printMessage3);
// Hello
// Noting to display
// null
// 0
//
printMessage(printMessage4);
// Hello
// Noting to display!
// Noting to display!
// Noting to display!
// Noting to display!
// Object Destructuring
const person = {
name: 'Julia',
age: 20,
phone: '01023456789'
}
function displayPerson1(person) {
console.log(person.name, person.age)
}
function displayPerson2(person) {
const name = person.name;
const age = person.age;
console.log(name, age)
}
function displayPerson3(person) {
const {name, age} = person;
console.log(name, age)
}
function displayPerson4({name, age} = person) {
console.log(name, age)
console.log(person)
}
displayPerson1(person);
displayPerson2(person);
displayPerson3(person);
displayPerson4(person);
// Spread Syntax
const item = { tpye: 'cloths' };
const detail = { size: '100', price: 20 };
const shirt0 = Object.assign(item, detail);
const shirt = { ...item, ...detail, price: 20 };
console.log(shirt0, shirt)
let fruits = ['흑', '백'];
fruits.push('적');
fruits = [...fruits, '청'];
fruits.unshift('황')
fruits = ['녹', ...fruits];
console.log(fruits)
fruits2 = ['투명']
console.log(fruits.concat(fruits2));
fruits = [...fruits, ...fruits2];
console.log(fruits)
// Optional Chaining
console.log(`# Optional Chaining #######################################`);
const bob = {
name: 'Julia',
age: 20,
};
const anna = {
name: 'Julia',
age: 20,
job: {
title: 'Software Engineer'
}
}
const displayJobTitle1 = (person) => {
if(person.job && person.job.title) {
console.log(person.job.title);
}
}
const displayJobTitle2 = (person) => {
if(person.job?.title) {
console.log(person.job.title);
}
}
const displayJobTitle3 = (person) => {
const title = person.job?.title ?? 'No Job Yet';
console.log(title)
}
displayJobTitle1(bob);
displayJobTitle2(bob);
displayJobTitle3(bob);
displayJobTitle1(anna);
displayJobTitle2(anna);
displayJobTitle3(anna);
// Template Literals
let raehan = 'raehan';
console.log('hello ' + raehan);
console.log(`hello ${raehan}`);
// 배열 중 짝수를 찾아 4를 곱한 후 다 더하기
let items = [1, 2, 3, 4, 5, 6];
let sum = items.filter(item => item%2===0).map(item => item*4).reduce((a, b) => a+b, 0);
console.log(sum);
const array = ['개', '냥', '말', '꿀', '꿀', '개'];
console.log(array);
console.log(new Set(array))
console.log([...new Set(array)]);
/*
https://m.ppomppu.co.kr/new/bbs_view.php?id=relay&no=557217
https://m.ppomppu1.co.kr/new/bbs_view.php?id=relay&no=560038
https://m.ppomppu1.co.kr/new/bbs_view.php?id=relay&no=567689
*/