[JavaScript] - ๊ธฐ๋ณธ ๋ฌธ๋ฒ (4)
๐ ์์ฑ์ ํจ์ / ํ๋กํ ํ์
์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ด๊ธฐํํ๋ ํจ์
- ์์ฑ์ ํจ์๋ ๋ด๋ถ์์
this
๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ๋ ๊ฐ์ฒด์ ์์ฑ ์ ์- ํ๋กํ ํ์ ์ ์ฌ์ฉํ์ฌ ๋ฉ์๋๋ฅผ ๊ณต์ ํ์ฌ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์
- ๋ชจ๋ ๊ฐ์ฒด๋ ํ๋กํ ํ์ ๊ฐ์ฒด๋ก๋ถํฐ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์์ ๋ฐ์
1
2
3
4
5
6
7
8
9
10
11
12
function Bellingham(born, height, club) {
this.born = born;
this.height = height;
this.club = club;
}
Bellingham.prototype.getPersonInfo = function() {
return `${this.born} ${this.height} ${this.club}`;
}
let myBellingham = new Car(`England`, `186cm`, `Real-Madrid`);
console.log(MyBellingham.getPersonInfo()); // England 186cm Real-Madrid
๐ ํด๋์ค
ํด๋์ค๋ ES6๋ถํฐ ๋์
ํ์๊ณ , ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ์ฝ๊ฒ ๊ตฌํํ ์ ์๊ฒ ํด์ค
1๏ธโฃ ํด๋์ค ์ ์ธ
1
2
3
4
5
6
7
8
9
10
11
12
13
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log('Hello, ' + this.name);
}
}
let ronaldo = new Person('Ronaldo', 40);
ronaldo.greet(); // Hello, Ronaldo
- โ๏ธ constructor๋ ๋ฐ๋์ ์์ด์ผํจ
2๏ธโฃ ํด๋์ค ์ ์ธ์ ํจ์ ์ ์ธ๊ณผ ๋ฌ๋ฆฌ ํธ์ด์คํ โ
1
2
3
4
5
6
7
8
const bellingham = new Person('Bellingham', 22); // ReferenceError: Cannot access 'Person' before initialization
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
3๏ธโฃ ์์
ํด๋์ค๋
extends
ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ํด๋์ค ์์ ๊ฐ๋ฅ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Employee extends Person {
constructor(name, age, job) {
super(name, age);
this.job = job;
}
work() {
console.log(this.name + ' is working as a ' + this.job);
}
}
let kane = new Employee('Kane', 32, 'soccer-player');
kane.greet(); // Hello, kane
kane.work(); // kane is working as a soccer-player
4๏ธโฃ static
static ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ํด๋์ค์ ์ธ์คํด์ค๊ฐ ์๋ ํด๋์ค ์์ฒด์ ์ํ๋ ๋ฉ์๋์ ์์ฑ์ ์ ์ํ ์ ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class UserService {
static async login(email, password) {
const isLogin(email, password){
console.log("๋ก๊ทธ์ธํ์
จ์ต๋๋ค")
}
return { email, password };
}
// ํ์๊ฐ์
static async signup(userData) {
const isSignup(userData){
console.log("ํ์๊ฐ์
ํ์
จ์ต๋๋ค")
}
return userData;
}
}
console.log(UserService.login("kanekane", 1234)); // ๋ก๊ทธ์ธํ์
จ์ต๋๋ค
console.log(UserService.signup({ name: "Bellingham", age: 22 })); // ํ์๊ฐ์
ํ์
จ์ต๋๋ค
5๏ธโฃ getter / setter
ํด๋์ค ๋ด ์์ฑ์ ์ ๊ทผ ๋ฐ ์ค์ ์ ํ ์ ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Person {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
set name(newName) {
if (newName) {
this._name = newName;
}
}
}
let person = new Person('Messi');
console.log(person.name); // Messi
person.name = 'Walker';
console.log(person.name); // Walker
6๏ธโฃ ์ค๋ฒ๋ผ์ด๋ฉ
์์ ํด๋์ค ๋ฉ์๋๋ฅผ ํ์ ํด๋์ค์ ์ฌ์ ์(์ค๋ฒ๋ผ์ด๋ฉ) ๊ฐ๋ฅ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Club {
name() {
console.log('Club Name');
}
}
class Soccer extends Club {
name() {
console.log('Manchester UTD');
}
}
let myClub = new Soccer();
myClub.name(); // Manchester UTD
๐ ๋ด์ฅ ๊ฐ์ฒด
1๏ธโฃ ํ์ค ๋ด์ฅ ๊ฐ์ฒด
ํ์คย ๋ด์ฅย ๊ฐ์ฒด๋ย ์๋ฐ์คํฌ๋ฆฝํธย ์์ง์ย ๊ธฐ๋ณธ์ผ๋กย ๋ด์ฅ๋์ด์๊ณ , ๋ณ๋ ์ ์ธ ์์ด ์ฌ์ฉ ๊ฐ๋ฅ
- Object
- Function
- Array
- String
- Boolean
- Number
- Math
- Date
- RegEXP
2๏ธโฃ ๋ฐฐ์ด ๋ด์ฅ ๊ฐ์ฒด
์ฃผ์ ๋ฉ์๋
push()
โก๏ธ ๋ฐฐ์ด ๋์ ์์ ์ถ๊ฐpop()
โก๏ธ ๋ฐฐ์ด ๋์ ์์ ์ ๊ฑฐshift()
โก๏ธ ๋ฐฐ์ด ์ฒซ ๋ฒ์งธ ์์ ์ ๊ฑฐunshift()
โก๏ธ ๋ฐฐ์ด ์์ ์์ ์ถ๊ฐjoin()
โก๏ธ ๋ฐฐ์ด์ ๋งค๊ฐ๋ณ์์ ์กฐํฉํ์ฌ ํฉ์นจsort
โก๏ธ ๋ฐฐ์ด ์ ๋ ฌreverse()
โก๏ธ ๋ฐฐ์ด ์ญ์์ผ๋ก ๋ค์ง์mpa()
,filter()
,reduce()
โก๏ธ ๋ฐฐ์ด ์ํํ์ฌ ์์ ์ํ
3๏ธโฃ ๋ฌธ์ ๋ด์ฅ ๊ฐ์ฒด
์ฃผ์ ๋ฉ์๋
charAt()
โก๏ธ ํน์ ์ธ๋ฑ์ค ๋ฌธ์ ๋ฐํsplit()
โก๏ธ ๋ฌธ์์ด์separator
๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํ์ฌ ๋ฐฐ์ด ๋ง๋ฌconcat()
โก๏ธ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋์ด์ง ๋ฌธ์์ด์ ํ๋์ ๋ฌธ์์ด๋ก ํฉ์นจindexOf()
โก๏ธ ํน์ ๋ฌธ์์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํincludes()
โก๏ธ ๋ฌธ์์ด์ ํน์ ๋ฌธ์์ด์ด ํฌํจ๋์ด ์๋์ง ํ์ธslice()
โก๏ธ ๋ฌธ์์ด์ ์ผ๋ถ๋ถ์ ์ถ์ถtoUpperCase()
,toLowerCase(()
โก๏ธ ๋์๋ฌธ์ ๋ณํsubstring()
โก๏ธ ๋ถ๋ถ ๋ฌธ์์ด ๋ฐํ
4๏ธโฃ Math ๊ฐ์ฒด
์ฃผ์ ๋ฉ์๋
Math.abs()
โก๏ธ ์ ๋๊ฐ ๋ฐํMath.ceil()
,Math.floor()
โก๏ธ ์ฌ๋ฆผ, ๋ด๋ฆผMath.max()
,Math.min()
โก๏ธ ์ต๋๊ฐ, ์ต์๊ฐ ๋ฐํMath.random()
โก๏ธ 0๊ณผ 1 ์ฌ์ด์ ๋์ ์์ฑMath.min()
โก๏ธ ์ฃผ์ด์ง ์ซ์ ์ค ์ ์ผ ๋ฎ์ ์ซ์๋ฅผ ๋ฐํMath.round()
โก๏ธ ์์์ ์ ๋ฐ์ฌ๋ฆผMath.floor()
โก๏ธ ์์์ ์ ๋ด๋ฆผMath.ceil()
โก๏ธ ์์์ ์ ์ฌ๋ฆผMath.abs()
โก๏ธ ์ ๋๊ฐ์ ๋ฐํMath.pow(x, y)
โก๏ธ x์ y ์ ๊ณฑ์ ๋ฐํ
๐ ๋๊ธฐ / ๋น๋๊ธฐ
๋๊ธฐ : ์ฝ๋๊ฐ ์์ฐจ์ ์ผ๋ก ์คํ, ์ด์ ์ฝ๋ ์๋ฃ์ ๊น์ง ๋ค์ ์ฝ๋ ์คํ X
๋น๋๊ธฐ : ์ฝ๋๊ฐ ๋ณ๋ ฌ๋ก ์คํ, ๋ค๋ฅธ ์ฝ๋ ์คํ ๋ฐฉํด X
1๏ธโฃ ์ฝ๋ฐฑํจ์
์ฝ๋ฐฑ ํจ์๋ ๋ค๋ฅธ ํจ์์ ์ธ์๋ก ์ ๋ฌ๋์ด ์คํ๋๋ ํจ์
- ๋๊ธฐ์ ์ฝ๋ฐฑ(synchronous callback)
- ํธ์ถ๋๋ ์ฆ์ ์คํ๋๊ณ , ๊ทธ ์์ ์ด ์๋ฃ๋ ๋๊น์ง ๋ค์ ์ฝ๋๋ฅผ ์คํ โ
1 2 3 4 5 6 7 8 9 10 11 12 13 14
function syncCallback(callback) { console.log("์ฝ๋ฐฑ ์ "); callback(); console.log("์ฝ๋ฐฑ ํ"); } syncCallback(() => { console.log("๋ด๋ถ ์ฝ๋ฐฑ"); }); // ์ถ๋ ฅ: // ์ฝ๋ฐฑ ์ // ๋ด๋ถ ์ฝ๋ฐฑ // ์ฝ๋ฐฑ ํ
- ๋น๋์ ์ฝ๋ฐฑ(asynchromous callback)
- ๋น๋๊ธฐ์ ์ฝ๋ฐฑ์ ํธ์ถ๋ ํ ๋น๋๊ธฐ ์์ ์ด ์๋ฃ๋ ํ์ ์คํ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
function asyncCallback(callback) { console.log("์ฝ๋ฐฑ ์ "); setTimeout(() => { callback(); console.log("๋ด๋ถ ์ฝ๋ฐฑ"); }, 1000); console.log("์ฝ๋ฐฑ ํ"); } asyncCallback(() => { console.log("์ฝ๋ฐฑ ์คํ๋จ"); }); // ์ถ๋ ฅ: // ์ฝ๋ฐฑ ์ // ์ฝ๋ฐฑ ํ // 1์ด ํ // ์ฝ๋ฐฑ ์คํ๋จ // ๋ด๋ถ ์ฝ๋ฐฑ
2๏ธโฃ Promise Then
Promise๋ ๋น๋๊ธฐ ์์
์ ์๋ฃ ๋๋ ์คํจ๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด, then ๋ฉ์๋ ์ฌ์ฉํ์ฌ ์ฒ๋ฆฌ๋ฅผ ์ ์
Promise๋ 3๊ฐ์ ์ํ๋ฅผ ๊ฐ์ง
- ๋๊ธฐ : ์ด๊ธฐ์ํ
- ์ดํ : ์ฐ์ฐ ์๋ฃ
- ๊ฑฐ๋ถ : ์ฐ์ฐ ์คํจ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let promise = new Promise((resolve, reject) => {
let success = true; // ์์
์ฑ๊ณต ์ฌ๋ถ
setTimeout(() => {
if (success) {
resolve('Data fetched successfully'); // ์ดํ ์ํ
} else {
reject('Error in fetching data'); // ๊ฑฐ๋ถ ์ํ
}
}, 2000);
});
>> then ๋ฉ์๋ ์ฒด์ด๋
- ์ฒซ ๋ฒ์งธ ์ธ์๋ ํ๋ก๋ฏธ์ค๊ฐ ์ดํ๋ ๊ฒฝ์ฐ์ ํธ์ถ๋๋ ์ฝ๋ฐฑ ํจ์
- ๋ ๋ฒ์งธ ์ธ์๋ ๊ฑฐ๋ถ๋ ๊ฒฝ์ฐ์ ํธ์ถ๋๋ ์ฝ๋ฐฑ ํจ์
- ๊ฐ then์ ์๋ก์ด ํ๋ก๋ฏธ์ค ๊ฐ์ฒด๋ฅผ ๋ฐํํ์ฌ ์ฒด์ด๋์ ๊ฐ๋ฅํ๊ฒ ํจ
promise
.then((data) => {
console.log(data); // Data fetched successfully
})
.catch((error) => {
console.error(error); // Error in fetching data
});
3๏ธโฃ async await
async await๋ Promise Then์ ํธํ๊ฒ ์ฌ์ฉํ๊ฒ ํด์ค
- async๋ ํญ์ Promise๋ฅผ ๋ฐํ
1
2
3
4
5
6
async function fetchData() {
// ์ด ํจ์๋ Promise๋ฅผ ๋ฐํ
return 'Data fetched';
}
fetchData().then(data => console.log(data)); // Data fetched
- await๋ Promise๊ฐ ์ฒ๋ฆฌ๋ ๋๊น์ง ํจ์ ์คํ์ ์ค์ง์์ผ์ค
1
2
3
4
5
6
7
8
9
10
async function fetchData() {
let data = await new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Data fetched');
}, 2000); 2์ด ์ค์ง ์์ผ์ค
});
console.log(data); // Data fetched
}
fetchData();