Skip to content

Commit 7f3da35

Browse files
authored
๐Ÿ”€: Udemy/Ts/section5/62: private (#38)
* ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: README์„ ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: issue templates ์ƒ์„ฑ Github์—์„œ ์ž‘์„ฑํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: pr-template ์ƒ์„ฑ Github์—์„œ ์ž‘์„ฑํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: pr-template ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐ŸŒฑ: pr-template ์ˆ˜์ • Github์—์„œ ์ˆ˜์ •ํ•œ ์ปค๋ฐ‹์ž…๋‹ˆ๋‹ค. * ๐Ÿšฉ: udemy section2์˜ 14๋ฒˆ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์‹ค์Šต์„ ํ–ˆ๋‹ค. * ๐Ÿšฉ: interface๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ํด๋ž˜์Šค๋ฅผ ํ•™์Šตํ•˜๊ณ  ์‹ค์Šตํ•ด๋ณด์•˜๋‹ค. * ๐Ÿšฉ: dist์˜ ์ปดํŒŒ์ผ๋œ js๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ es5์—์„œ es6๋กœ ts.config ๋ณ€๊ฒฝ * ๐Ÿšฉ: es6์—์„œ es5๋กœ ์ปดํŒŒ์ผ target์„ ๋ณ€๊ฒฝ * ๐Ÿšฉ: this๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅํ•œ class ๋‚ด ์ „์—ญ name ํ”„๋กœํผํ‹ฐ ์ •์ƒ ์ถœ๋ ฅ * ๐Ÿšฉ: thisํ‚ค์›Œ๋“œ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฐ์ฒด๋กœ ๊ฐ์‹ผ ๋ณต์ œ ํ•จ์ˆ˜ ํ• ๋‹น ํ›„ ์ถœ๋ ฅ undefined ๊ฒฐ๊ณผ ํ™•์ธ, this๋ฅผ ์‚ฌ์šฉ์‹œ ์ฃผ์˜ ์‚ฌํ•ญ * ๐Ÿšฉ: undefined ์žฌํ™•์ธ * ๐Ÿšฉ: ๋ณต์ œํ•œ class์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•๊ณผ ์—๋Ÿฌ ์ž„์‹œ์ ์ธ ํ•ด๊ฒฐ ๋‹จ, ๋˜‘๊ฐ™์ด ๊ตฌํ˜„ ๋ถˆ๊ฐ€๋Šฅ * ๐Ÿšฉ: ๋ฐฐ์—ด ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹ * ๐Ÿšฉ: private ์„ค์ •์œผ๋กœ employees[N]์— ๊ฐ’์„ ํ• ๋‹นํ• ์ˆ˜ ์—†๋‹ค. error * ๐Ÿšฉ: ๋Ÿฐํƒ€์ž„๊ณผ ์ปดํŒŒ์ผ ๊ณผ์ •์˜ private ์˜ํ–ฅ์„ ts, js ํŒŒ์ผ์—์„œ ํ™•์ธ
1 parent ea3c4be commit 7f3da35

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

โ€Ždist/app.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
"use strict";
22
var Department = /** @class */ (function () {
33
function Department(n) {
4+
this.employees = [];
45
this.name = n;
56
}
6-
77
Department.prototype.describe = function () {
88
console.log("Department: " + this.name);
99
};
10+
Department.prototype.addEmployee = function (employee) {
11+
this.employees.push(employee);
12+
};
13+
Department.prototype.printEmployeeInformation = function () {
14+
console.log(this.employees.length);
15+
console.log(this.employees);
16+
};
1017
return Department;
1118
}());
1219
var accounting = new Department('Accounting');
20+
accounting.addEmployee('Max');
21+
accounting.addEmployee('Manu');
22+
accounting.employees[2] = 'Anna'; //employee private ์„ค์ •์œผ๋กœ error ๋ฐœ์ƒ
23+
//์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•ด๋„ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘์„ ํ•˜์ง€๋งŒ ๋ณต์žกํ•œ ์ฝ”๋“œ์—์„œ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์€ ์ข‹์€ ๋ฐฉ์‹์ด ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ employee๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ ํ•ด์•ผํ•œ๋‹ค. ๋”ฐ๋ผ ์ ‘๊ทผ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋Š” private๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
24+
//TSError: โจฏ Unable to compile TypeScript: src / app.ts: 26: 12 - error TS2341: Property 'employees' is private and only accessible within class 'Department'.
25+
// private๋Š” class ๋‚ด์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ณ  ์™ธ๋ถ€์—์„œ๋Š” ์ ‘๊ทผํ• ์ˆ˜ ์—†๋‹ค.
26+
// ๋‹จ, ํ•ด๋‹น error ๋ถ€๋ถ„์€ ์ปดํŒŒ์ผํ•  ๋•Œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์ด ๋™์ž‘ํ•ด์„œ app.js์—์„œ ํ™•์ธ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋Ÿฐํƒ€์ž„ ๊ณผ์ •์—์„œ๋Š” error๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
1327
accounting.describe();
14-
var accountingCopy = { name: 'newName', describe: accounting.describe };
15-
//์—ฌ๊ธฐ์„œ undefined๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ด์œ ๋Š” describe๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
16-
//๋”ฐ๋ผ์„œ accountingCopy์™€ ๊ฐ™์ด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ์žฌ์ƒ์„ฑ์„ ํ•ด์„œ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” class์˜ describeํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—
17-
//this๋ฅผ ๋„ฃ๊ณ  ํƒ€์ž…์ง€์ •์„ Department๋กœ ์„ค์ •ํ•œ ๋’ค(์ฆ‰, ์ฒญ์‚ฌ์ง„์„ ์ฐธ์กฐํ•˜๊ฒŒ ์„ค์ •) accountingCopy์— name ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ธฐ์ž…, ํ• ๋‹นํ•ด์ค˜์•ผ ํ•œ๋‹ค.
18-
accountingCopy.describe();
28+
accounting.printEmployeeInformation();

โ€Žpackage.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "app.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8+
"watch": "tsc -w",
89
"start": "ts-node src/app.ts",
910
"build": "tsc --build",
1011
"clean": "tsc --build --clean"

โ€Žsrc/app.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
class Department {
22
name: string;
3+
private employees: string[] = [];
34
constructor(n: string) {
45
this.name = n;
56
}
6-
77
describe(this: Department) {
88
console.log(`Department: ` + this.name)
99
}
10+
11+
addEmployee(employee: string) {
12+
this.employees.push(employee);
13+
}
14+
15+
printEmployeeInformation() {
16+
console.log(this.employees.length);
17+
console.log(this.employees);
18+
}
1019
}
1120

1221
const accounting = new Department('Accounting');
13-
accounting.describe();
22+
accounting.addEmployee('Max');
23+
accounting.addEmployee('Manu');
1424

15-
const accountingCopy = { name: 'newName', describe: accounting.describe };
16-
//์—ฌ๊ธฐ์„œ undefined๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ด์œ ๋Š” describe๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
17-
//๋”ฐ๋ผ์„œ accountingCopy์™€ ๊ฐ™์ด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ์žฌ์ƒ์„ฑ์„ ํ•ด์„œ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” class์˜ describeํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—
18-
//this๋ฅผ ๋„ฃ๊ณ  ํƒ€์ž…์ง€์ •์„ Department๋กœ ์„ค์ •ํ•œ ๋’ค(์ฆ‰, ์ฒญ์‚ฌ์ง„์„ ์ฐธ์กฐํ•˜๊ฒŒ ์„ค์ •) accountingCopy์— name ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ธฐ์ž…, ํ• ๋‹นํ•ด์ค˜์•ผ ํ•œ๋‹ค.
19-
20-
accountingCopy.describe();
25+
accounting.employees[2] = 'Anna'; //employee private ์„ค์ •์œผ๋กœ error ๋ฐœ์ƒ
26+
//์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•ด๋„ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘์„ ํ•˜์ง€๋งŒ ๋ณต์žกํ•œ ์ฝ”๋“œ์—์„œ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์€ ์ข‹์€ ๋ฐฉ์‹์ด ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ employee๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ ํ•ด์•ผํ•œ๋‹ค. ๋”ฐ๋ผ ์ ‘๊ทผ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋Š” private๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
27+
//TSError: โจฏ Unable to compile TypeScript: src / app.ts: 26: 12 - error TS2341: Property 'employees' is private and only accessible within class 'Department'.
28+
// private๋Š” class ๋‚ด์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ณ  ์™ธ๋ถ€์—์„œ๋Š” ์ ‘๊ทผํ• ์ˆ˜ ์—†๋‹ค.
29+
// ๋‹จ, ํ•ด๋‹น error ๋ถ€๋ถ„์€ ์ปดํŒŒ์ผํ•  ๋•Œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์ด ๋™์ž‘ํ•ด์„œ app.js์—์„œ ํ™•์ธ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋Ÿฐํƒ€์ž„ ๊ณผ์ •์—์„œ๋Š” error๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
30+
accounting.describe();
31+
accounting.printEmployeeInformation();

0 commit comments

Comments
ย (0)