Skip to content

Commit 51b7756

Browse files
authored
๐Ÿ”€: Udemy/Ts/section5/63: private with constructor (#39)
* ๐ŸŒฑ: 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 ํŒŒ์ผ์—์„œ ํ™•์ธ * ๐Ÿšฉ: private๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ constructor์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• * ๐Ÿšฉ: constructor ์ƒ์„ฑ์ž ๋‚ด๋ถ€์— ๋”ฐ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•
1 parent 7f3da35 commit 51b7756

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

โ€Ždist/app.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
"use strict";
22
var Department = /** @class */ (function () {
3-
function Department(n) {
3+
function Department(id, name) {
4+
this.id = id;
5+
this.name = name;
6+
// private id: string;
7+
// private name: string;
48
this.employees = [];
5-
this.name = n;
9+
// this.id = id;
10+
// this.name = n
611
}
712
Department.prototype.describe = function () {
8-
console.log("Department: " + this.name);
13+
console.log("Department (".concat(this.id, "): ").concat(this.name));
914
};
1015
Department.prototype.addEmployee = function (employee) {
1116
this.employees.push(employee);
@@ -16,13 +21,9 @@ var Department = /** @class */ (function () {
1621
};
1722
return Department;
1823
}());
19-
var accounting = new Department('Accounting');
24+
var accounting = new Department('1', 'Accounting');
2025
accounting.addEmployee('Max');
2126
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๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
27+
// accounting.employees[2] = 'Anna';
2728
accounting.describe();
28-
accounting.printEmployeeInformation();
29+
accounting.printEmployeeInformation();

โ€Žsrc/app.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
class Department {
2-
name: string;
2+
// private id: string;
3+
// private name: string;
34
private employees: string[] = [];
4-
constructor(n: string) {
5-
this.name = n;
5+
6+
constructor(private id: string, public name: string) {
7+
// this.id = id;
8+
// this.name = n
69
}
710
describe(this: Department) {
8-
console.log(`Department: ` + this.name)
11+
console.log(`Department (${this.id}): ${this.name}`);
912
}
1013

1114
addEmployee(employee: string) {
1215
this.employees.push(employee);
1316
}
14-
1517
printEmployeeInformation() {
1618
console.log(this.employees.length);
1719
console.log(this.employees);
1820
}
1921
}
2022

21-
const accounting = new Department('Accounting');
23+
const accounting = new Department('1', 'Accounting');
24+
2225
accounting.addEmployee('Max');
2326
accounting.addEmployee('Manu');
2427

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๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
28+
// accounting.employees[2] = 'Anna';
3029
accounting.describe();
3130
accounting.printEmployeeInformation();

0 commit comments

Comments
ย (0)